OkHttpClient 升级到 OkHttp3 后抛出异常
OkHttpClient throws exception after upgrading to OkHttp3
我正在使用以下代码行将默认 header 添加到我使用 Retrofit2 发送的所有请求中:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
static {
defaultHttpClient.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Accept", "Application/JSON").build();
return chain.proceed(request);
}
});
}
将 retrofit 升级到 beta-3 版本后,我还必须将 OkHttp 升级到 OkHttp3(实际上我只是将包名从 okhttp 更改为 okhttp3,该库包含在 retrofit 中)。之后我从这一行得到异常:
defaultHttpClient.networkInterceptors().add(new Interceptor());
Caused by: java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:932)
Caused by: java.lang.ExceptionInInitializerError
这里有什么问题?
如果你想创建 OkHttp(3)Client 对象,你必须使用构建器。
尝试改变这个:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
像这样:
OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
.addInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Accept", "Application/JSON").build();
return chain.proceed(request);
}
}).build();
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'
您可能应该使用这些版本。只需放置它们,同步您的 gradle,删除所有导入,然后重试。
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
我正在使用以下代码行将默认 header 添加到我使用 Retrofit2 发送的所有请求中:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
static {
defaultHttpClient.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Accept", "Application/JSON").build();
return chain.proceed(request);
}
});
}
将 retrofit 升级到 beta-3 版本后,我还必须将 OkHttp 升级到 OkHttp3(实际上我只是将包名从 okhttp 更改为 okhttp3,该库包含在 retrofit 中)。之后我从这一行得到异常:
defaultHttpClient.networkInterceptors().add(new Interceptor());
Caused by: java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:932)
Caused by: java.lang.ExceptionInInitializerError
这里有什么问题?
如果你想创建 OkHttp(3)Client 对象,你必须使用构建器。
尝试改变这个:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
像这样:
OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
.addInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Accept", "Application/JSON").build();
return chain.proceed(request);
}
}).build();
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'
您可能应该使用这些版本。只需放置它们,同步您的 gradle,删除所有导入,然后重试。
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;