okhttp,Connection

时间:2018-07-31 18:03:58

标签: android okhttp

我目前正在学习android,而okhttp3遇到了一些问题。 这是堆栈跟踪。

07-31 17:57:54.933 23100-24099/com.ullxfeg.networktest W/System.err: java.io.IOException: unexpected end of stream on Connection{10.0.2.2:8099, proxy=DIRECT hostAddress=/10.0.2.2:8099 cipherSuite=none protocol=http/1.1}
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-31 17:57:54.934 23100-24099/com.ullxfeg.networktest W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-31 17:57:54.935 23100-24099/com.ullxfeg.networktest W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at com.ullxfeg.networktest.MainActivity$1.run(MainActivity.java:60)
07-31 17:57:54.936 23100-24099/com.ullxfeg.networktest W/System.err:     at java.lang.Thread.run(Thread.java:764)
    Caused by: java.io.EOFException: \n not found: limit=0 content=…
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:237)
        at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        ... 17 more

我的工作环境是Apache 2.4.34,Windows 10,Android Studio 3.1.3和minSdkVersion22。OKHTTP版本是3.11.0

我的示例代码是:

private void sendRequestWithOKHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient.Builder()
                            .retryOnConnectionFailure(true)
                            .build();
                    Request request = new Request.Builder()
                            .url("http://10.0.2.2:8099/get_data.xml")
                            .addHeader("Connection", "close")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseXMLWithPull(responseData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

我首先不小心解决了Apache httpd.conf文件的问题:

# Various default settings
Include conf/extra/httpd-default.conf

# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
#Include conf/extra/httpd-proxy-html.conf
</IfModule>

但是后来这个问题每次都会发生,我不知道发生了什么。 有什么解决办法吗?

0 个答案:

没有答案