D/OkHttp: <-- HTTP FAILED: java.net.SocketException: Socket closed
Retrofit+Rxjava has no error callback.
Please provide a test case or sample that reproduces the issue.
Same error.
I used the same request in postman with the same parameters and it work fine, now I'm using retrofit:2.3.0-adapter, rxjava2:2.3.0 and io.reactivex.rxjava2:rxandroid:2.0.1 and it doesn't work
REQUEST DATA
D/OkHttp: --> POST http://192.168.100.6:4000/api/v1/class/answers http/1.1
D/OkHttp: Content-Type: application/json; charset=UTF-8
D/OkHttp: Content-Length: 1429
D/OkHttp: Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJMaXZlUXVpekJlV2ViIiwiZXhwIjoxNTU0NDE3ODY4LCJpYXQiOjE1MjI4ODE4NjgsImlzcyI6IkxpdmVRdWl6QmVXZWIiLCJqdGkiOiJmNGEwYWNkNC1iOWY4LTRhOGEtYTM2Ni1hN2E3ZmI2MTQzZTEiLCJuYmYiOjE1MjI4ODE4NjcsInN1YiI6IjIiLCJ0eXAiOiJhY2Nlc3MifQ.TqKneqtlqhJ7ZhRBow-JkY5aB70r4QbCRamyz8t_c7jzksSmPUUwycpDrhrjPMng0TRvljGHV4POYfWI3Vsr2g
D/OkHttp: {"answers":[{"answer_value":0,"classes_id":5,"correct_answer_id":184,"question_id":46,"selected_answer_id":181,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":0,"classes_id":5,"correct_answer_id":186,"question_id":47,"selected_answer_id":185,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":1,"classes_id":5,"correct_answer_id":190,"question_id":48,"selected_answer_id":190,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":0,"classes_id":5,"correct_answer_id":196,"question_id":49,"selected_answer_id":195,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":0,"classes_id":5,"correct_answer_id":202,"question_id":51,"selected_answer_id":203,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":1,"classes_id":5,"correct_answer_id":205,"question_id":52,"selected_answer_id":205,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":0,"classes_id":5,"correct_answer_id":211,"question_id":53,"selected_answer_id":212,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2},{"answer_value":1,"classes_id":5,"correct_answer_id":225,"question_id":57,"selected_answer_id":225,"series_question_count":15,"test_type_id":2,"tests_series_id":10,"user_id":2}]}
D/OkHttp: --> END POST (1429-byte body)
D/OkHttp: <-- HTTP FAILED: java.net.SocketException: Socket closed
I have put doOnError, doOnComplete, subscribe and nobody catch the success or error
I also encountered it. It was normal after restarting the phone.
I am have this problem,how to solve this problem?
D/OkHttp: <-- HTTP FAILED: java.net.SocketException: Socket closed
Retrofit+Rxjava has no error callback.
@LeeTech @JakeWharton
I encountered this as well. I am using Observable.interval() to generate an observable that will call into the retrofit observables every minute. The first call is successful. It seems to be the successive ones that cause the problems.
We face this problem too, and Search through the internet, found a way to reproduce this problem,
The reason of this kind exception is Cancel operation.
if you use Retrofit + RxJava, when you call Subscription#unsubscribe, it will call Call#cancel, maybe you will face this exception.
Detail as below
we got a full stack message.
java
java.net.SocketException: Socket closed
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:189)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at okio.Okio$2.read(Okio.java:138)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:236)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:344)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:216)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:191)
at okhttp3.RealCall.execute(RealCall.java:69)
reproduce code
`
public class CancelCall {
private static final String TAG = "CancelCall";
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
private final OkHttpClient client = new OkHttpClient();
public void run(int delay) throws Exception {
Request request = new Request.Builder()
.url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
.build();
final long startNanos = System.nanoTime();
final Call call = client.newCall(request);
// Schedule a job to cancel the call in 1 second.
executor.schedule(new Runnable() {
@Override public void run() {
Log.d(TAG, String.format("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f));
call.cancel();
Log.d(TAG, String.format("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f));
}
}, delay, TimeUnit.MILLISECONDS);
Log.d(TAG, String.format("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f));
try (Response response = call.execute()) {
Log.d(TAG, String.format("%.2f Call was expected to fail, but completed: %s%n",
(System.nanoTime() - startNanos) / 1e9f, response));
} catch (IOException e) {
Log.d(TAG, String.format("%.2f Call failed as expected: %s%n",
(System.nanoTime() - startNanos) / 1e9f, e));
}
}
}
calling code is
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 20; ++i) {
try {
new CancelCall().run(i * 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
I'm also having this issue as well. As stated above, it occurs in consecutive calls.
I'm not using Retrofit but OkHttp3. Same issue: after 30 seconds or so using Observable.interval() to get periodic updates from a WiFi peripheral, Android app crashes with a "Socket closed" error coming from an onFailure() callback. Then I get "FATAL EXCEPTION: The exception was not handled due to missing onError handler in the subscribe() method call."
Doubt it's relevant, but my Emitter (basically, Subscriber) sets a Disposable that calls .cancel() on the HTTP Call if the Single request is disposed.
Update: after some experimenting, it seems that the call to .cancel() is what's causing the crash. The Logcat shows that .dispose() is being called not once, but twice for every request. I don't know what's doing this鈥攊t's not my code, which never calls it. I'm now testing .isExecuted() (it's actually enqueued) for the call before attempting to .cancel() it. It always says true, so no more calling .cancel().
@lincleejun that looks like a normal exception that might occur from cancelation. And that code doesn't contain any Retrofit so there's not anything that Retrofit can do.
Most helpful comment
I encountered this as well. I am using Observable.interval() to generate an observable that will call into the retrofit observables every minute. The first call is successful. It seems to be the successive ones that cause the problems.