gpt4 book ai didi

android - 改造 API 调用收到 "HTTP FAILED: java.io.IOException: Canceled"

转载 作者:IT老高 更新时间:2023-10-28 22:24:42 26 4
gpt4 key购买 nike

无法弄清楚为什么会发生这种情况。我的调用不会触发任何一个 rx 回调(onCompleted()、onError()、onNext())。我唯一收到的是这个 okhttp 输出:

D/OkHttp: --> GET https://api.privatbank.ua/p24api/exchange_rates?json=true&date=20.11.2016 http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled

改造模块:

@Module
public class RestModule {

@Provides
@Singleton
public HttpLoggingInterceptor providesHttpLogginInterceptor() {
return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
}

@Provides
@Singleton
public OkHttpClient providesOkHttpClient(@NonNull HttpLoggingInterceptor loggingInterceptor) {
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(ConstantsManager.CONNECTION_TIME_OUT, TimeUnit.SECONDS)
.readTimeout(ConstantsManager.READ_TIME_OUT, TimeUnit.SECONDS)
.build();
}

@Provides
@Singleton
public Gson providesGson() {
return new GsonBuilder().create();
}

@Provides
@Singleton
public Retrofit providesRetrofit(@NonNull OkHttpClient okHttpClient, @NonNull Gson gson) {
return new Retrofit.Builder()
.baseUrl(ConstantsManager.BASE_URL)
.client(okHttpClient)
.addConverterFactory(SimpleXmlConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}

@Provides
@Singleton
public PrivatbankApi providesPrivatbankApi(@NonNull Retrofit retrofit) {
return retrofit.create(PrivatbankApi.class);
}
}

API接口(interface):

public interface PrivatbankApi {

@GET
Observable<CurrentRates> loadCurrentRates(@NonNull @Url String url);

@GET("exchange_rates")
Observable<DateRates> loadDateRates(@NonNull @Query("json") Boolean json, @NonNull @Query("date") String date);

}

订阅:

subscription = dataManager.loadDateRates(date)
.subscribeOn(Schedulers.io())
.doAfterTerminate(() -> {
})
.subscribe(dateRates -> {
// My code here...
}, throwable -> {
Timber.e(throwable, "Error while loading data occurred!");
});

顺便说一句,两个调用都得到相同的错误:

D/OkHttp: --> GET https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ua http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled
D/OkHttp: --> GET https://api.privatbank.ua/p24api/exchange_rates?json=true&date=20.11.2016 http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled

最佳答案

如果用户取消请求,则会引发该异常。使用 RxJavaCallAdapterFactory 时,如果在调用完成之前取消订阅,就会发生这种情况。所以我猜在你打电话后的某个时候,你会做 subscription.unsubscribe() 这会取消底层的请求。

关于android - 改造 API 调用收到 "HTTP FAILED: java.io.IOException: Canceled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40823134/

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