问题: 尝试“连接”时,我有一个间歇但频繁的失败。 代码,调试消息和堆栈跟踪如下。我看过类似的 问题答案,以及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
答案 0 :(得分:2)
我很确定这与您的代码无关,只有您可以使用特定请求重现它。
ECONNRESET表示服务器(也可以是防火墙等)关闭连接。
我有大约50%的请求曾经遇到过同样的问题,结果证明群集的一方有关于虚拟IP映射的硬件缺陷。因此,当负载均衡器决定将请求传递到左侧时,它会起作用,而右侧则会失败。
尝试找出您的请求与成功/失败之间是否存在某种关联。也许某种应用程序防火墙在执行某些请求时会丢失您的连接。也许是拒绝服务保护......
如果成功/失败绝对是随机的,那么Android手机和Yahoo服务器之间的设备可能会损坏。
您可以跟踪从提交请求到获得响应所需的时间。失败的请求总是需要更多时间吗?我可以想象你正在调用的REST服务的Web服务器的队列可能会在连接完全时丢弃连接。这也可以通过负载均衡器或防火墙来完成,因为大多数Web服务器都会使用HTTP Code 503进行应答。