THE PROBLEM: I have an intermittent, but frequent, failure when attempting a "connect". The code, debug messages and stack trace is below. I have looked at the similar question answers, and the android developer help, but cannot find an answer to the failure.
Any help is appreciated.
THE CODE:
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
I'm quite sure that this has nothing to do with your code, only if you can reproduce it with a particular request.
ECONNRESET means connection was closed by the server (which can also be a firewall, etc.).
I had the same problem once with about 50% of the requests and it turned out that one side of a cluster had hardware defects concerning virtual IP mapping. So when the load balancer decided to pass the request to the left side, it worked, and to the right side it failed.
Try to find out whether there is some correlation between your request and success/failure. Maybe some kind of application firewall drops your connection when doing certain requests. Perhaps a denial-of-service protection...
If success/failure is absolutely random, then probably a device between your Android phone and the Yahoo server is broken.
And you could track the time it takes from submitting the request until you get a response. Do the failing requests always take more time? I could imagine that the queue of the webservers of the REST service you're calling might drop connections when it's full. This could also be done by a load balancer or firewall, because most web servers would answer with HTTP Code 503.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With