3
問題: 嘗試「連接」時,我有一個間歇但頻繁的故障。 代碼,調試消息和堆棧跟蹤如下。我曾看過類似的 問題的答案,以及android開發人員的幫助,但無法找到 答案的失敗。
任何幫助表示讚賞。
的代碼:
public String doInBackground (String... urlArray) {
String string;
InputStream is = null;
int responseCode = 0;
Log.d("Data", "doInBackground ");
try {
URL url = new URL(urlArray[0]);
// Create a new HTTP URL connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Log.d("Data", "connection= " + connection);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setDoInput(true);
connection.connect();
responseCode = connection.getResponseCode();
Log.d("Data", "responseCode= " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();
int len=50000;
string = readIt(is, len);
connection.disconnect();
is.close();
return string;
}
else {
connection.disconnect();
return "File Not Found";
}
}
catch (IOException e) {
Log.d("Data", "e.getCause()= " + e.getCause());
Log.d("Data", "e.getMessage()= " + e.getMessage());
e.printStackTrace();
return null;
}
}
Debug messages when it works: 01-23 11:57:00.029: D/Data(21987): doInBackground 01-23 11:57:00.029: D/Data(21987): connection= com.android.okhttp.internal.http.HttpURLConnectionImpl:http://finance.yahoo.com/d/quotes.csv?s=^GSPC+^GSPTSE+^IXIC+^NDX+^TNX&f=snxl1d1t1c1ohgv&ignore=.csv 01-23 11:57:00.549: D/Data(21987): responseCode= 200 01-23 11:57:00.549: D/Data(21987): readIt start 01-23 11:57:00.609: D/Data(21987): readIt end 01-23 11:57:00.609: D/Data(21987): onPostExecute start 01-23 11:57:00.629: D/Data(21987): onPostExecute end Debug messages when it fails: ============================= 01-23 11:57:03.839: D/Data(21987): doInBackground 01-23 11:57:03.839: D/Data(21987): connection= com.android.okhttp.internal.http.HttpURLConnectionImpl:http://finance.yahoo.com/d/quotes.csv?s=^GSPC+^GSPTSE+^IXIC+^NDX+^TNX&f=snxl1d1t1c1ohgv&ignore=.csv 01-23 11:57:05.939: D/Data(21987): e.getCause()= libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer) 01-23 11:57:05.939: D/Data(21987): e.getMessage()= recvfrom failed: ECONNRESET (Connection reset by peer) 01-23 11:57:05.979: D/Data(21987): onPostExecute start 01-23 11:57:06.009: D/Data(21987): onPostExecute end The print stack trace: ============================= 01-23 11:57:05.939: W/System.err(21987): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 01-23 11:57:05.949: W/System.err(21987): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545) 01-23 11:57:05.949: W/System.err(21987): at libcore.io.IoBridge.recvfrom(IoBridge.java:509) 01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 01-23 11:57:05.949: W/System.err(21987): at java.io.InputStream.read(InputStream.java:162) 01-23 11:57:05.949: W/System.err(21987): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142) 01-23 11:57:05.949: W/System.err(21987): at java.io.BufferedInputStream.read(BufferedInputStream.java:227) 01-23 11:57:05.949: W/System.err(21987): at com.android.okhttp.internal.Util.readAsciiLine(Util.java:316) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503) 01-23 11:57:05.959: W/System.err(21987): at com.retepcivorodot.stockcharts.QuoteListFragment$MyAsyncTask.doInBackground(QuoteListFragment.java:949) 01-23 11:57:05.959: W/System.err(21987): at com.retepcivorodot.stockcharts.QuoteListFragment$MyAsyncTask.doInBackground(QuoteListFragment.java:1) 01-23 11:57:05.959: W/System.err(21987): at android.os.AsyncTask$2.call(AsyncTask.java:288) 01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 01-23 11:57:05.969: W/System.err(21987): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 01-23 11:57:05.969: W/System.err(21987): at java.lang.Thread.run(Thread.java:841) 01-23 11:57:05.969: W/System.err(21987): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer) 01-23 11:57:05.979: W/System.err(21987): at libcore.io.Posix.recvfromBytes(Native Method) 01-23 11:57:05.979: W/System.err(21987): at libcore.io.Posix.recvfrom(Posix.java:141) 01-23 11:57:05.979: W/System.err(21987): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164) 01-23 11:57:05.979: W/System.err(21987): at libcore.io.IoBridge.recvfrom(IoBridge.java:506) 01-23 11:57:05.979: W/System.err(21987): ... 21 more