2016-05-21 66 views
1

夥計們請嘗試從改造1改爲改造2但我很難爲所有請求添加請求攔截器。 在改造1,我通過做這樣的事情來實現它;改造2:添加RequestInterceptor

public static <S> S createService(Class<S> serviceClass, final String token) { 
    RestAdapter.Builder builder = new RestAdapter.Builder() 
      .setEndpoint(Constant.APP_URL); 
    builder.setRequestInterceptor(new RequestInterceptor() { 
     @Override 
     public void intercept(RequestFacade request) { 
      request.addHeader("token", token); 
     } 
    }); 
    RestAdapter adapter = builder.build(); 

    return adapter.create(serviceClass); 
} 

但我還是不能讓我解決這個頭改造2 ...請沒有任何人有一個解決我的問題?

回答

0

在你的依賴塊添加:

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' 

在你Retrofit服務類插件:

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient client = new OkHttpClient.Builder() 
      .addInterceptor(interceptor) 
      .build(); 

然後加入:

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(URL) 
      .client(client) // this is the line you care about 
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
      .addConverterFactory(JacksonConverterFactory.create()) 
      .build(); 
+0

不錯我會試試看 –

+0

@DhãtDhøpëBwøūyÇüzzy ,我也可以幫助你使用Stetho攔截器。讓我知道。 –

+0

好的,我會讓你知道我試過這個 –