改装API调用收到"HTTP失败:java.io.IOException:已取消". [英] Retrofit API call receives "HTTP FAILED: java.io.IOException: Canceled"

查看:2954
本文介绍了改装API调用收到"HTTP失败:java.io.IOException:已取消".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么会这样. rx回调之一(onCompleted(),onError(),onNext())都不会被我的调用触发.我唯一收到的是这个okhttp输出:

Can't figure out why is this happening. Neither one of rx callbacks (onCompleted(), onError(), onNext()) not gets triggered by my call. The only thing i receive is this okhttp output:

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接口:

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!");
                });

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

By the way, both of the calls gets the same error:

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()来取消基础请求.

That exception gets thrown if the request is cancelled by the user. When using RxJavaCallAdapterFactory this happens if the subscription is unsubscribed before the call can complete. So I guess at some point after you do the call you do subscription.unsubscribe() which cancels the underlying requests.

这篇关于改装API调用收到"HTTP失败:java.io.IOException:已取消".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆