2017-04-03 117 views
0

每一件事情都正常工作當我的應用程序進入狀態,然後到onResume,並且當我調用任何API時,我會遇到錯誤。HTTP失敗:java.io.IOException:在多窗口模式下取消

HTTP失敗:java.io.IOException異常:取消

我使用RxJava和改造做我的所有網絡操作。以下是我的設置。

ApplicationComponent.java

@ApplicationScope 
@Component(modules = {ContextModule.class, NetworkModule.class}) 
public interface ApplicationComponent { 
    void inject(MyApplication myApplication); 
} 

ApplicationScope.java

@Scope 
@Retention(RetentionPolicy.CLASS) 
    public @interface ApplicationScope { 
} 

ContextModule.java

@Module 
public class ContextModule { 

    private Context context; 

    public ContextModule(Context context){ 
     this.context = context; 
    } 

    @Provides 
    @ApplicationScope 
    public Context applicationContext(){ 
     return context.getApplicationContext(); 
    } 

} 

NetworkModule.java

@Module @ApplicationScope 
public class NetworkModule { 

    private final String BASE_CONTACT_URL = ""; 

    @Provides @ApplicationScope 
    public PoolAPIService getPoolApiService(Retrofit retrofit){ 
     APIServiceapiServicece = retrofit.create(APIService.class); 
     return apiServicece; 
    } 

    @Provides @ApplicationScope 
    public Retrofit getRetrofit(OkHttpClient okHttpClient){ 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_CONTACT_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
       .client(okHttpClient) 

       .build(); 

     return retrofit; 
    } 

    @Provides @ApplicationScope 
    public OkHttpClient getOkHttpClient(HttpLoggingInterceptor interceptor, Cache cache){ 
     OkHttpClient okhttpclient = new OkHttpClient.Builder() 
       .addInterceptor(interceptor) 
       .cache(cache) 
       .build(); 
     return okhttpclient; 
    } 

    @Provides @ApplicationScope 
    public HttpLoggingInterceptor getInterceptor(){ 
     HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { 
      @Override 
      public void log(String message) { 
       Log.d("Log", message); 
      } 
     }); 
     interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
     return interceptor; 
    } 

    @Provides @ApplicationScope 
    public Cache getCache(File cacheFile){ 
     Cache cache = new Cache(cacheFile, 10*1024*1024); //10MB size 
     return cache; 
    } 

    @Provides @ApplicationScope 
    public File getCacheFile(Context context){ 
     File cacheFile = new File(context.getCacheDir(), "cache"); 
     cacheFile.mkdirs(); 
     return cacheFile; 
    } 

    @Provides @ApplicationScope 
    public Picasso getPicasso(Context context, OkHttpClient okHttpClient){ 
     Picasso picasso = new Picasso.Builder(context) 
       .downloader(new OkHttp3Downloader(okHttpClient)) 
       .build(); 
     return picasso; 
    } 

} 

我在我的Application類中注入它。

MyApplication.java

public class MyApplication extends Application { 

    ApplicationComponent applicationComponent; 
    @Inject 
    APIService apiService; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     applicationComponent = DaggerApplicationComponent.builder() 
       .contextModule(new ContextModule(this)) 
       .build(); 

     applicationComponent.inject(this); 
    } 

    public APIService getAPIService(){ 
     return apiService; 
    } 

} 

這是我如何調用API服務。

LoginActivity.java

//creating LoginService instance 
LoginService loginService = new LoginService(((MyApplication)getApplication()).getAPIService()); 

....... 

void login() { 
     final String mobileNumber = view.getMobileNumber(); 
     final String password = view.getPassword(); 

     Subscriber<LoginResponse> subscriber = new Subscriber<LoginResponse>() { 
      @Override 
      public void onCompleted() { 
       Log.d(TAG, "onCompleted"); 
       if(subscriptionLogin != null && !subscriptionLogin.isUnsubscribed()){ 
        subscriptionLogin.unsubscribe(); 
       } 
      } 

      @Override 
      public void onError(Throwable e) { 
       Log.e(TAG, "error: " + e.getMessage()); 
      } 

      @Override 
      public void onNext(LoginResponse loginResponse) { 
       Log.d(TAG, loginResponse.message); 
      } 
     }; 


     LoginRequest request = new LoginRequest(); 
     request.mobileNumber = mobileNumber; 
     request.password = password; 

     subscriptionLogin = service.login(subscriber, request); 

     compositeSubscription.add(subscriptionLogin); 

.... 

    void onDestroy() { 
    //unsubscribe all the subscription 
    compositeSubscription.unsubscribe(); 
} 

logcat的

--> POST http://.../.. http/1.1 
Content-Type: application/json; charset=UTF-8 
Content-Length: 32 
--> END POST (32-byte body) 
<-- HTTP FAILED: java.io.IOException: Canceled 
+0

你用'compositeSubscription'做了什麼? – azizbekian

+0

請發佈完整的錯誤stacktrace –

+0

你在做什麼onPause()和onResume()? – yosriz

回答

1

我登錄onStartonResume,和onStop。下面是輸出活動時進入多窗口模式:

enter image description here

我敢打賭,你有沒有預期後onResume立即將被調用。而且,只要您在onResume中創建訂閱,您立即在處取消訂閱是合理的。

HTTP失敗:java.io.IOException異常:取消

這意味着,你必須從你的訂閱退訂(即你的改裝呼叫被取消)。

+0

還有onStop中的compositeSubscription.unsubscribe()。謝謝你讓我看着正確的方向。 – ik024

+1

@ ik024,歡迎光臨。意想不到的行爲,至少對我而言。 – azizbekian

相關問題