什么导致HTTP失败:java.net.SocketException:套接字closures?

我正在使用okhttp和改造我的应用程序之一,我有很多请求使用RxJava在不同的线程上工作。 有时我得到任何请求和后缀没有请求可以达到服务器的SocketException。

例如,我可以分享一个样本。

很less修改代码片段可以分享。

Subscription details = api.details("keyword") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Model>() { @Override public void onCompleted() { Timber.d("onCompleted(): "); } @Override public void onError(Throwable e) { Timber.e(e, "onError(): "); } @Override public void onNext(final Model details) { Timber.d("onNext(): "); } }); 

没有任何从3个callback中调用。

Stackstace:

 11-17 15:50:53.991 16595-10314/ D/OkHttp: --> GET API_REQUEST_URL 11-17 15:50:53.991 16595-10314/ D/OkHttp: Host: HOST_NAME 11-17 15:50:53.991 16595-10314/ D/OkHttp: Connection: Keep-Alive 11-17 15:50:53.991 16595-10314/ D/OkHttp: Accept-Encoding: gzip 11-17 15:50:53.991 16595-10314/ D/OkHttp: User-Agent: okhttp/3.4.2 11-17 15:50:53.991 16595-10314/ D/OkHttp: User-Agent: Android-App 11-17 15:50:53.991 16595-10314/ D/OkHttp: --> END GET 11-17 15:50:53.992 16595-10314/ D/OkHttp: <-- HTTP FAILED: java.net.SocketException: Socket closed 

其他IOException

 11-17 16:36:04.274 28523-29137/ D/OkHttp: Host: HOST_NAME 11-17 16:36:04.274 28523-29137/ D/OkHttp: Connection: Keep-Alive 11-17 16:36:04.274 28523-29137/ D/OkHttp: Accept-Encoding: gzip 11-17 16:36:04.274 28523-29137/ D/OkHttp: User-Agent: okhttp/3.4.2 11-17 16:36:04.274 28523-29137/ D/OkHttp: User-Agent: Android-App 11-17 16:36:04.274 28523-29137/ D/OkHttp: --> END GET 11-17 16:36:04.282 28523-29137/ D/OkHttp: <-- HTTP FAILED: java.io.IOException: unexpected end of stream on okhttp3.Address@6924d4a0 

订阅我试图打电话工作正常,但有时上面的错误是在那里,然后停止工作。 Aferwords没有请求可以达到服务器。

从改造界面的方法

 @GET("/v1/app_endpoint/{keyword}/") Observable<Model> details(@Path("keyword") String keyword); 

如果需要的话,下面几个辅助方法只是用来检查configuration。

 public static Retrofit retrofit() { return provideRetrofit(provideGson(), provideOkHttpClient()); } public static Gson provideGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.addSerializationExclusionStrategy(new AnnotationExclusionStrategy()); gsonBuilder.addDeserializationExclusionStrategy(new AnnotationExclusionStrategy()); gsonBuilder.setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getDeclaringClass().equals(RealmObject.class); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); gsonBuilder.registerTypeAdapterFactory(AutoValueGsonTypeAdapterFactory.create()); return gsonBuilder.create(); } public static OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(600, TimeUnit.SECONDS) .readTimeout(1, TimeUnit.MINUTES) .retryOnConnectionFailure(true) .addNetworkInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Content-Type", "application/json") .addHeader("User-Agent", "Android-App") .build(); return chain.proceed(newRequest); } }) .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); } public static Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { return new Retrofit.Builder() .baseUrl(BuildConfig.API_URL) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .build(); } 

依赖

 // Networking compile 'com.squareup.okhttp3:okhttp:3.4.2' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' 

Solutions Collecting From Web of "什么导致HTTP失败:java.net.SocketException:套接字closures?"

在我的情况下,我发现我得到了一个突然意外的unsubscribe()调用,导致HTTP FAILED: java.net.SocketException: Socket closed错误,并没有调用任何的onCompleted() onError()onNext()callback – 检查讨论并在这里回答。

所以,为了能够处理这种情况,只需添加:

 .doOnUnsubscribe(() -> { /*handle logic here*/ }) 

给你的Observable ,也许试着重新设置一些状态,希望能够恢复正常的活动。

在我的情况下,我要求一个错误的URL获取错误ERR_CONNECTION_REFUSED