首页 > 代码库 > Android HttpsUrlConnection eofexception --> java.io.EOFException

Android HttpsUrlConnection eofexception --> java.io.EOFException

05-15 17:32:51.624: W/System.err(9851): java.io.EOFException
05-15 17:32:51.694: W/System.err(9851): 	at libcore.io.Streams.readAsciiLine(Streams.java:203)
05-15 17:32:51.694: W/System.err(9851): 	at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
05-15 17:32:51.694: W/System.err(9851): 	at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
05-15 17:32:51.694: W/System.err(9851): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
05-15 17:32:51.694: W/System.err(9851): 	at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
05-15 17:32:51.694: W/System.err(9851): 	at org.ksoap2.transport.ServiceConnectionSE.getResponseCode(ServiceConnectionSE.java:103)
05-15 17:32:51.694: W/System.err(9851): 	at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:197)
05-15 17:32:51.694: W/System.err(9851): 	at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
05-15 17:32:51.694: W/System.err(9851): 	at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
05-15 17:32:51.694: W/System.err(9851): 	at cn.mr.qrcode.remote.WebService.getResponse(WebService.java:66)
05-15 17:32:51.694: W/System.err(9851): 	at cn.mr.qrcode.service.RemoteService.getCustomerInfo(RemoteService.java:825)
05-15 17:32:51.694: W/System.err(9851): 	at cn.mr.qrcode.view.customer.CustomerInfoActivity$7.doInBackground(CustomerInfoActivity.java:325)
05-15 17:32:51.694: W/System.err(9851): 	at cn.mr.qrcode.view.customer.CustomerInfoActivity$7.doInBackground(CustomerInfoActivity.java:1)
05-15 17:32:51.757: W/System.err(9851): 	at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-15 17:32:51.757: W/System.err(9851): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-15 17:32:51.757: W/System.err(9851): 	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-15 17:32:51.757: W/System.err(9851): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-15 17:32:51.757: W/System.err(9851): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-15 17:32:51.757: W/System.err(9851): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-15 17:32:51.757: W/System.err(9851): 	at java.lang.Thread.run(Thread.java:856)
最近在使用WebService请求服务端数据是偶尔会报EOFException,从网上差了一些资料,转载自:http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception :
<p style="margin-top: 0px; margin-bottom: 1em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 14px; vertical-align: baseline; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 17.804800033569336px;">I have a problem where my HttpsURLConnection will throw an EOFException when i try to read any input. The code works for some network calls, but fails on others. If i try and read anything from the connection, it fails with the aforementioned error.</p><p style="margin-top: 0px; margin-bottom: 1em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 14px; vertical-align: baseline; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 17.804800033569336px;">Example:</p><pre class="default prettyprint prettyprinted" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.804800033569336px;"><code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">urlConnect</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">getResponseCode</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">()</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 128, 128);">// will throw error</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">
urlConnect</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">getResponseMessage</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">()</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 128, 128);">// will throw error</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">
</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">BufferedInputStream</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">in</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">new</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">BufferedInputStream</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">urlConnect</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">getInputStream</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">());</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 128, 128);">//will throw error</span></code>

Here is the stack trace for each:

getResponse:

03-14 09:49:18.547: W/System.err(6270): java.io.EOFException
03-14 09:49:18.547: W/System.err(6270):     at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)

BufferedInputStream:

03-14 09:39:14.077: W/System.err(5935): java.io.EOFException
03-14 09:39:14.077: W/System.err(5935):     at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-14 09:39:14.077: W/System.err(5935):     at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
03-14 09:39:14.077: W/System.err(5935):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
03-14 09:39:14.077: W/System.err(5935):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
03-14 09:50:46.547: W/System.err(6476):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
03-14 09:50:46.547: W/System.err(6476):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)

Thank you for any help,

Rick

EDIT I found my answer:

This was not a well documented answer. It appears in some of the newer versions of android, there is a bug with recycled url connections. To fix this (although there may be some performance issues), I needed to add:

if (Build.VERSION.SDK != null
&& Build.VERSION.SDK_INT > 13) {
urlConnect.setRequestProperty("Connection", "close");
}

Thanks!

Rick


看他们的分析是,这是Java的某些版本在回收httpconnection的一个Bug,通过添加如下判断,设置httpConnection的属性,可以解决这个问题:
if (Build.VERSION.SDK != null&& Build.VERSION.SDK_INT > 13) {urlConnect.setRequestProperty("Connection", "close");}