2016-11-26 245 views
4

無法弄清楚爲什麼會發生這種情況。沒有一個rx回調函數(onCompleted(),onError(),onNext())不會被我的調用觸發。我收到的唯一的事情是這樣的okhttp輸出:改進API調用收到「HTTP失敗:java.io.IOException:取消」

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

順便說一句,這兩個調用都會得到相同的錯誤:

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

我有同樣的問題,當它移動到一次性,並把它放在意向服務類的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 
*/ 
相關問題