2016-08-02 10 views
5

kullanırken, Hançer 2.0 ile çoklu uç noktaları nasıl desteklenir Bir Android projesi için, aşağıdaki Hançer modülünde gösterildiği gibi, 2.1.0 ve OkHttp 3.4.1'i Hançer 2.6 ile yapılandırdım. @Named niteleyicileri kullanarak birden çok arka uçları desteklemeyi hedefliyorum.MockWebServer

@Module 
public class ApiModule { 

    private static final String GITHUB_QUALIFIER = "GitHub"; 

    private static final String TWITTER_QUALIFIER = "Twitter"; 

    @Provides 
    GitHubClient provideGitHubClient(@Named(GITHUB_QUALIFIER) Retrofit retrofit) { /* ... */ } 

    @Provides 
    TwitterClient provideTwitterClient(@Named(TWITTER_QUALIFIER) Retrofit retrofit) { /* ... */ } 

    @Named(GITHUB_QUALIFIER) 
    @Provides 
    @Singleton 
    HttpUrl provideGitHubBaseUrl() { /* ... */ } 

    @Named(TWITTER_QUALIFIER) 
    @Provides 
    @Singleton 
    HttpUrl provideTwitterBaseUrl() { /* ... */ } 

    @Named(GITHUB_QUALIFIER) 
    @Provides 
    @Singleton 
    Retrofit getGitHubRetrofit(@Named(GITHUB_QUALIFIER) HttpUrl httpUrl, 
           OkHttpClient okHttpClient) { /* ... */ } 

    @Named(TWITTER_QUALIFIER) 
    @Provides 
    @Singleton 
    Retrofit getTwitterRetrofit(@Named(TWITTER_QUALIFIER) HttpUrl httpUrl, 
           OkHttpClient okHttpClient) { /* ... */ } 

    private Retrofit getRetrofit(String baseUrl, 
           OkHttpClient okHttpClient) { /* ... */ } 

    @Provides 
    @Singleton 
    public OkHttpClient provideOkHttpClient() { /* ... */ } 

} 

Test için MockWebServer kullanmak istiyorum. Ancak, ben aynı anda birden fazla arka uçları desteklerken ben MockWebServer URL'sini geçebilir nasıl bulamıyor:

// From a unit test 

mockWebServer = new MockWebServer(); 
mockWebServer.start(); 
ApiModule apiModule = new ApiModule(); 
OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); 

String url = mockWebServer.url("/").toString(); 
// Here I cannot pass the URL to Retrofit 
Retrofit retrofit = apiModule.provideRetrofit(/* HERE */ okHttpClient); 

gitHubClient = apiModule.provideGitHubClient(retrofit); 

cevap

0

O okhttp interceptor ile baz url geçersiz kılmak mümkündür.

public final class HostSelectionInterceptor implements Interceptor { 
    private volatile String mHost; 

    public void setHost(String host) { 
     this.mHost = checkNotNull(host); 
    } 

    @Override 
    public okhttp3.Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     String host = this.mHost; 
     if (host != null) { 
      HttpUrl overriddenBaseUrl = HttpUrl.parse(host); 
      HttpUrl newUrl = request.url() 
        .newBuilder() 
        .host(overriddenBaseUrl.host()) 
        .build(); 
      request = request.newBuilder() 
        .url(newUrl) 
        .build(); 
     } 
     return chain.proceed(request); 
    } 
} 

buradan Alındığı: Sen MockWebServer url beslemek için bu önleme kullanmak ve hatta ayıklama kurar için evreleme uç noktaları arasında geçiş yapabilirsiniz https://gist.github.com/swankjesse/8571a8207a5815cca1fb

.