I'm using following lines of code to add a default header to all of my requests sent using Retrofit2:
我正在使用以下代码行为我使用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);
}
});
}
After upgrading retrofit to beta-3 version, I had to upgrade OkHttp to OkHttp3 also (actually I just changed package names from okhttp to okhttp3, the library is included inside retrofit). After that I get exceptions from this line:
在将改造升级到beta-3版本之后,我还必须将OkHttp升级到OkHttp3(实际上我只是将包名称从okhttp更改为okhttp3,该库包含在改造中)。之后我从这一行得到例外:
defaultHttpClient.networkInterceptors().add(new Interceptor());
Caused by: java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:932)
引起:java.util.Collections的java.lang.UnsupportedOperationException $ UnmodifiableCollection.add(Collections.java:932)
Caused by: java.lang.ExceptionInInitializerError
引起:java.lang.ExceptionInInitializerError
What is the problem here?
这里有什么问题?
2 个解决方案
#1
62
You have to use builder if you want to create OkHttp(3)Client object.
如果要创建OkHttp(3)Client对象,则必须使用构建器。
Try change this:
尝试改变这个:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
To something like this:
对于这样的事情:
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();
#2
1
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'
You should probably use these versions. Just put them, sync your gradle, delete all imports, and try again.
你应该使用这些版本。只需放置它们,同步您的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;
#1
62
You have to use builder if you want to create OkHttp(3)Client object.
如果要创建OkHttp(3)Client对象,则必须使用构建器。
Try change this:
尝试改变这个:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
To something like this:
对于这样的事情:
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();
#2
1
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'
You should probably use these versions. Just put them, sync your gradle, delete all imports, and try again.
你应该使用这些版本。只需放置它们,同步您的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;