添加标题的所有请求与改造2
翻新2的文档说:
需要添加到每个请求的头可以使用OkHttp拦截器来指定。
使用以前的版本可以轻松完成, 这里是相关的QA。
但使用retrofit 2,我找不到像setRequestInterceptor
或setInterceptor
方法,可以应用到Retrofit.Builder
对象。
另外似乎OkHttp中没有RequestInterceptor
了。 Retrofit的文档提到了Interceptor ,我不太明白如何使用它。
我怎样才能做到这一点?
OkHttpClient httpClient = new OkHttpClient(); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader("parameter", "value").build(); return chain.proceed(request); } }); Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(url).client(httpClient).build();
最新翻新版本HERE – > 2.1.0。
拉姆达版本:
builder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); });
丑陋的长版本:
builder.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); } });
完整版:
class Factory { public static APIService create(Context context) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); builder.readTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(5, TimeUnit.SECONDS); if (BuildConfig.DEBUG) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(interceptor); } builder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); }); builder.addInterceptor(new UnauthorisedInterceptor(context)); OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(APIService.ENDPOINT).client(client).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); return retrofit.create(APIService.class); } }
gradle文件(如果您打算使用它,则需要添加日志拦截器):
//----- Retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' compile "com.squareup.retrofit2:converter-gson:2.1.0" compile "com.squareup.retrofit2:adapter-rxjava:2.1.0" compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
为了logging你的请求和响应,你需要一个拦截器,并且为了设置你需要的拦截器的头文件,下面是使用翻新2.1添加拦截器的解决scheme
public OkHttpClient getHeader(final String authorizationValue ) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient okClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .addNetworkInterceptor( new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = null; if (authorizationValue != null) { Log.d("--Authorization-- ", authorizationValue); Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .addHeader("Authorization", authorizationValue); request = requestBuilder.build(); } return chain.proceed(request); } }) .build(); return okClient; }
现在在你的改造对象中添加这个头在客户端
Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .client(getHeader(authorizationValue)) .addConverterFactory(GsonConverterFactory.create()) .build();
在我的情况下, addInterceptor()
无法将HTTP头添加到我的请求,我不得不使用addNetworkInterceptor()
。 代码如下:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addNetworkInterceptor(new AddHeaderInterceptor());
拦截器代码:
public class AddHeaderInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); builder.addHeader("Authorization", "MyauthHeaderContent"); return chain.proceed(builder.build()); } }
这个和这个要点更多的例子
如果使用addInterceptor方法添加HttpLoggingInterceptor,则不会logging由HttpLoggingInterceptor之后应用的其他拦截器添加的内容。
例如:如果您有两个拦截器“HttpLoggingInterceptor”和“AuthInterceptor”,并且首先应用了HttpLoggingInterceptor,那么您将无法查看由AuthInterceptor设置的http-params或头。
OkHttpClient.Builder builder = new OkHttpClient.Builder() .addNetworkInterceptor(logging) .addInterceptor(new AuthInterceptor());
我解决了它,通过使用addNetworkInterceptor方法。