Retrofit 2.0 GET请求 报错 ECONNRESET (Connection reset by peer)

我的应用使用 Retrofit 2.0 调用服务端的api接口,我在wifi网络下测试都好了,然后用4G网络测试就报了这个错误。而且是只有GET请求有这个错误 POST请求是好的。

代码:

Request request = chain.request();
                Response response = null;
                int tryCount = 0;
                do {
                    tryCount++;
                    try {
                        response = chain.proceed(request.newBuilder().addHeader("Authorization", token)
                                .addHeader("Content-Type", "application/json;charset=UTF-8")
                                .addHeader("Connection", "keep-alive")
                                .build());
                    } catch (IOException e) {
                        //异常在这里报的
                        e.printStackTrace();
                    }
                } while ((response==null || !response.isSuccessful()) && tryCount < 3);

                return response;

具体异常信息:

06-28 13:32:07.718 30243-30322/ W/System.err: java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
06-28 13:32:07.720 30243-30322/ W/System.err:     at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
06-28 13:32:07.720 30243-30322/ W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
06-28 13:32:07.720 30243-30322/ W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
06-28 13:32:07.720 30243-30322/ W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
06-28 13:32:07.720 30243-30322/ W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okio.Okio$2.read(Okio.java:139)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
06-28 13:32:07.720 30243-30322/ W/System.err:     at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.RealCall.getResponse(RealCall.java:241)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
06-28 13:32:07.721 30243-30322/ W/System.err:     at .core.api.RetrofitRestClient$2.intercept(RetrofitRestClient.java:120)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
06-28 13:32:07.721 30243-30322/ W/System.err:     at okhttp3.RealCall.execute(RealCall.java:57)
06-28 13:32:07.721 30243-30322/ W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
06-28 13:32:07.721 30243-30322/ W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
06-28 13:32:07.721 30243-30322/ W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:211)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:205)
06-28 13:32:07.722 30243-30322/ W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
06-28 13:32:07.722 30243-30322/ W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.Observable$2.call(Observable.java:162)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.Observable$2.call(Observable.java:154)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:8314)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
06-28 13:32:07.722 30243-30322/ W/System.err:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
06-28 13:32:07.722 30243-30322/ W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
06-28 13:32:07.722 30243-30322/ W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-28 13:32:07.722 30243-30322/ W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
06-28 13:32:07.722 30243-30322/ W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
06-28 13:32:07.723 30243-30322/ W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-28 13:32:07.723 30243-30322/ W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-28 13:32:07.723 30243-30322/ W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-28 13:32:07.723 30243-30322/ W/System.err: Caused by: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
阅读 10k
2 个回答

you can like this
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏