2017-04-03 5 views
0

Alles funktioniert einwandfrei Ich stehe unter dem Fehler, wenn meine App geht zu Zustand und dann zu onResume und wenn ich irgendeine API aufrufen, bekomme ich unter Fehler.HTTP FAILED: java.io.IOException: Im Multiwindow-Modus abgebrochen

HTTP FAILED: java.io.IOException: Cancelled

I RxJava verwende und Retrofit alle Operationen mein Netzwerk zu tun. Unten ist mein Setup.

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; 
    } 

} 

Ich injiziere es in meiner Anwendungsklasse.

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; 
    } 

} 

Und das ist, wie ich die API-Service nenne.

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

Was machen Sie mit 'compositeSubscription'? – azizbekian

+0

Bitte posten Sie den vollständigen Fehler stacktrace –

+0

Was machst du bei onPause() und onResume()? – yosriz

Antwort

1

I onStart angemeldet haben, onResume, und onStop. Hier ist die Ausgabe, wenn die Aktivität Multi-Fenster-Modus eintritt:

enter image description here

Ich wette, Sie haben nicht zu erwarten, dass nach onResume sofort aufgerufen. Und solange Sie ein Abonnement in onResume erstellen, ist es angemessen, dass Sie sich in abmelden, was sofort passiert.

HTTP FAILED: java.io.IOException: Cancelled

Das bedeutet, dass Sie von Ihrem Abonnement unsubscribed haben (das heißt Ihr Retrofit Anruf wird abgebrochen).

+0

gab es compositeSubscription.unsubscribe() in onStop auch. Danke, dass Sie mich in die richtige Richtung geschaut haben. – ik024

+1

@ ik024, willkommen. Unerwartetes Verhalten, zumindest für mich. – azizbekian

Verwandte Themen