HTTP 失败:java.net.SocketException:套接字已关闭;不会启动异常处理方法 [英] HTTP FAILED: java.net.SocketException: Socket closed; doesn't fire up exception handling methods

查看:184
本文介绍了HTTP 失败:java.net.SocketException:套接字已关闭;不会启动异常处理方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RxJava 链请求应该释放一些 onError() 或 onComplete() 锁,所以,基本上,我的问题是:当我为 OkHttpClient 设置读取、连接和写入超时时,我没有得到期望的行为.我正在使用 Retrofit2 和 OkHttp3.6.0 这是我的简化客户端:

I have an RxJava chain request that should release some locks onError(), or onComplete(), so, basically, what my problem is: when I set read, connect and write timeouts to my OkHttpClient, I don't get the desired behavior. I'm using Retrofit2 and OkHttp3.6.0 Here's my simplified client:

OkHttpClient.Builder builder = new OkHttpClient.Builder()
           .readTimeout(30, TimeUnit.SECONDS)
           .connectTimeout(30, TimeUnit.SECONDS)
           .writeTimeout(30, TimeUnit.SECONDS)
OkHttpClient okHttpClient = builder.build();

这是我拥有的链式请求的简化版本:

Here's a simplified version of the chain request I have:

public <T extends Response> Observable<T> doSomething(Observable<T> base) {
    isLocked = true;
    return someApiCall()
               .flatMap(apiResponse -> handleResponse(apiResponse, base)
                   .doOnError(throwable -> {
                       isLocked = false;
                   })
                   .doOnCompleted(() -> {
                       isLocked = false;
                   }));
}

handleResponse() 进行另一个 API 调用并返回一个 Observable> 但是,正如我所说,它有时会因 HTTP FAILED: java.net.SocketException: Socket closed 并且它永远不会完成 Observable,因此永远不会调用 onError() 或 onComplete().我也试过 onTerminate() ,但没有运气.当我从 OkHttlClient 中删除超时设置时,实际上会抛出并捕获 SocketException,从而释放 isLocked 变量.我试过用 try {} catch (Exception e) {} 块包装 handleResponse() 返回语句,但即使这样也没有捕获 SocketException 设置自定义超时时.有什么想法吗?

handleResponse() makes another API call and returns an Observable<Response<Something>> but, as I've said, it sometimes fails with a HTTP FAILED: java.net.SocketException: Socket closed and it never finishes the Observable, so, onError() or onComplete() are never called. I've tried onTerminate() also, but with no luck. When I remove the timeout settings from the OkHttlClient, the SocketException is actually thrown and caught which releases the isLocked variable. I've tried wrapping the handleResponse() return statement with a try {} catch (Exception e) {} block, but even that doesn't catch the SocketException when the custom timeouts are set. Any ideas?

推荐答案

原来是请求被取消订阅,导致出现HTTP FAILED: java.net.SocketException: Socket closed 错误,因此,解决方案只是将此代码添加到链中:

It turns out that the request was getting unsubscribed, which caused the appearance of the HTTP FAILED: java.net.SocketException: Socket closed error, so, the solution was just adding this code to the chain:

.doOnUnsubscribe(() -> {
    isLocked = false;
})

我仍然不明白为什么自定义超时值会触发 SocketException,但我认为套接字正在等待超时到期并关闭,或者完成请求并关闭,但是它被 unsubscribe 调用中断,因此意外关闭.

I still don't understand why the custom timeout value was triggering the SocketException, but I suppose that the socket was waiting for the timeout to expire and close, or complete the request and close, but it got interrupted by the unsubscribe call and thus closed unexpectedly.

这篇关于HTTP 失败:java.net.SocketException:套接字已关闭;不会启动异常处理方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆