2016-11-26 33 views
4

これはなぜ発生しているのかわかりません。 rxコールバック(onCompleted()、onError()、onNext())のどちらも私の呼び出しによって起動されません。Retrofit APIコールが「HTTP FAILED:java.io.IOException:Cancelled」を受信しました

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

、私が受け取る唯一のことは、このokhttp出力されます両方の呼び出しで同じエラーが発生する:

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 

答えて

26

リクエストがユーザーによってキャンセルされた場合、その例外がスローされます。 RxJavaCallAdapterFactoryを使用している場合は、通話が完了する前に購読が登録解除されている場合に発生します。だから私はいつかあなたがsubscription.unsubscribe()の基本的な要求をキャンセルする呼び出しを行うと思います。

+0

私はdisposableに移動してインテントサービスクラスのondestroyクラスに入れ、 'disposable.dispose()'を呼び出すと同じ問題が発生します。 –

0

@Kiskaeに感謝します。これは私に正しいヒントを与えました。私の場合は、CompositeSubscriptionを使用して、別の方法で登録を解除した後にサブスクリプションを追加しました。

/** 
* Adds a new {@link Subscription} to this {@code CompositeSubscription} if the 
* {@code CompositeSubscription} is not yet unsubscribed. If the {@code CompositeSubscription} <em>is</em> 
* unsubscribed, {@code add} will indicate this by explicitly unsubscribing the new {@code Subscription} as 
* well. 
* 
* @param s 
*   the {@link Subscription} to add 
*/ 
関連する問題