gpt4 book ai didi

java - 不支持的操作 : Android, 改造,OkHttp。在 OkHttpClient 中添加拦截器

转载 作者:IT老高 更新时间:2023-10-28 20:26:05 47 4
gpt4 key购买 nike

我正在尝试使用拦截器在 Android 中通过 Retrofit 2.0-beta3 和 OkHttpClient 添加基于 token 的身份验证。但是当我在 OkHttpClient 中添加我的拦截器时,我得到了 UnsupportedOperationException。这是我的代码:在 ApiClient.java 中

public static TrequantApiInterface getClient(final String token) {
if( sTreqantApiInterface == null) {

Log.v(RETROFIT_LOG, "Creating api client for the first time");
OkHttpClient okClient = new OkHttpClient();

okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", token)
.method(original.method(), original.body());

Request request = requestBuilder.build();
return chain.proceed(request);
}
});

Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
sTreqantApiInterface = client.create(TrequantApiInterface.class);
}
return sTreqantApiInterface;
}

我会像这样使用它:

private void ampFreqTest(){
String token = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE)
.getString(getString(R.string.key_token), "");

service = ApiClient.getClient(token);
//I get an exception on this line:
Call<List<AmpFreq>> call = service.getUserAmpFreq("1");
call.enqueue(new Callback<List<AmpFreq>>() {
@Override
public void onResponse(Response<List<AmpFreq>> response) {
Toast.makeText(HomeScreen.this, "Got result", Toast.LENGTH_LONG);

Log.v(ApiClient.RETROFIT_LOG, "Success api client." + response.message());
Log.v(ApiClient.RETROFIT_LOG, "Success api client.");
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(HomeScreen.this, t.getMessage() , Toast.LENGTH_LONG);
Log.v(ApiClient.RETROFIT_LOG, "Fail api client." + t.getMessage() );
}
});
}

但我收到此错误:

Process: com.trequant.usman.trequant_android, PID: 14400
java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:932)
at com.trequant.usman.trequant_android.api.ApiClient.getClient(ApiClient.java:41)

添加新拦截器时出错,说它不是 modifiableCollection 但拦截器()函数的文档说:/**

   * Returns a modifiable list of interceptors that observe the full span of each call: from before
* the connection is established (if any) until after the response source is selected (either the
* origin server, cache, or both).
*/

我做错了什么?这可能是一个错误吗?

最佳答案

当您将 Retrofit 2.0-beta2 更改为 Retrofit 2.0-beta3 时会出现此问题。如果要创建 OkHttpClient 对象,则必须使用 builder。

改变:

 OkHttpClient okClient = new OkHttpClient();

okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", token)
.method(original.method(), original.body());

Request request = requestBuilder.build();
return chain.proceed(request);
}
});

到:

 OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", token)
.method(original.method(), original.body());

Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();

它应该可以解决您的问题。

关于java - 不支持的操作 : Android, 改造,OkHttp。在 OkHttpClient 中添加拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674820/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com