当我发送http请求时,它可以连接成功,但如果发送请求数据

时间:2017-09-06 02:02:49

标签: android okhttp3

在我的Android应用程序中,我们使用http请求从服务器获取数据,并使用okhttp3.0.1。 从日志中,我们发现一些用户网络是连接的。但是当它开始向服务器发送请求时,它可以找到健康的连接(不在池中,新建),但是如果发送请求数据则抛出异常。

java.net.SocketException: sendto failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
at libcore.io.IoBridge.sendto(IoBridge.java:468)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
at okio.Okio$1.write(Okio.java:78)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
at okio.RealBufferedSink.flush(RealBufferedSink.java:216)
at com.myapp.okhttp3.internal.http.Http1xStream.finishRequest(Http1xStream.java:162)
at com.myapp.okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:724)
at com.myapp.okhttp3.internal.http.HttpEngine.access$2(HttpEngine.java:723)
at com.myapp.okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:711)
at com.myapp.okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:566)
at com.myapp.okhttp3.RealCall.getResponse(RealCall.java:242)
at com.myapp.okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:199)
at com.myapp.okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:161)
at com.myapp.okhttp3.RealCall.execute(RealCall.java:58)

okhttp版本是3.0.1 任何人都可以告诉我它可能是什么问题?服务器,网络还是客户端? 我如何检查和修复它或者减少发生的可能性?

添加消息--------------------------------------------- --- 添加用于调用okhttp.Class的代码OkHttpStack覆盖在凌空中使用的HttpStack。

public HttpResponse performRequest(Request<?> request,
            Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
        int timeoutMs = request.getTimeoutMs();

        OkHttpClient.Builder builder = mClient.newBuilder();
        builder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)
                .readTimeout(timeoutMs, TimeUnit.MILLISECONDS)
                .writeTimeout(timeoutMs, TimeUnit.MILLISECONDS);

        OkHttpClient client = builder.build();

        com.ktcp.tencent.okhttp3.Request.Builder okHttpRequestBuilder = new com.ktcp.tencent.okhttp3.Request.Builder();
        String oUrl = request.getUrl();
        if(oUrl!=null && oUrl.indexOf("?")>=0)
        {
            oUrl = oUrl+"&timeforahj="+System.currentTimeMillis();
        }
        okHttpRequestBuilder.url(oUrl);

        Map<String, String> headers = request.getHeaders();
        for (final String name : headers.keySet()) {
            okHttpRequestBuilder.addHeader(name, headers.get(name));
        }
        for (final String name : additionalHeaders.keySet()) {
            // 这里用header方法,如果有重复的name,会覆盖,否则某些请求会被判定为非法
            okHttpRequestBuilder.header(name, additionalHeaders.get(name));
        }

        setConnectionParametersForRequest(okHttpRequestBuilder, request);

        com.ktcp.tencent.okhttp3.Request okHttpRequest = okHttpRequestBuilder.build();
        Call okHttpCall = client.newCall(okHttpRequest);
        Response okHttpResponse = null;
        try {
            okHttpResponse = okHttpCall.execute();

        } catch (IOException e) {

            throw e;
        }
        StatusLine responseStatus = new BasicStatusLine(
                parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(),
                okHttpResponse.message());
        BasicHttpResponse response = new BasicHttpResponse(responseStatus);
        response.setEntity(entityFromOkHttpResponse(okHttpResponse));

        Headers responseHeaders = okHttpResponse.headers();
        for (int i = 0, len = responseHeaders.size(); i < len; i++) {
            final String name = responseHeaders.name(i), value = responseHeaders.value(i);
            if (name != null) {
                response.addHeader(new BasicHeader(name, value));
            }
        }

        return response;
    }

大多数用户工作成功,但有些用户在连接wifi时重新调整socketexception。 当我添加一些日志时,我发现它可以连接server.this功能正常。连接是新创建,而不是连接池。

 private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
      int writeTimeout, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks)

但在上面发送请求为异常堆栈时抛出异常。 我的问题是连接是创建新的,意味着它可以连接到服务器,为什么它在发送请求时抛出异常。两个动作仍在继续。

0 个答案:

没有答案