HTTP FAILED: java.net.SocketException: Socket closed; doesn't fire up exception handling methods

て烟熏妆下的殇ゞ 提交于 2019-12-05 10:51:53

In Retrofit 2, exceptions below/above the HTTP layer are not reported as Response<?>, but as actual exceptions via onError.

In your case, you should move the doOnError/doOnCompleted out of the flatMap; as it is right now they respond to errors generated in the handleResponse only - but that method won't be called if someApiCall returns an error Observable.

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

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.

I solved this situation in Android with the option retryOnConnectionFailure(true) set with the builder.

Given that Retrofit 2 just receive the OkHttpClient object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!