I've a complex app which downloads plenty of content from my webservice on AWS. However, I keep getting SocketTimeoutException
50% of the time. Based on my research, I suspect that there might be following reasons:
- Time for connection timeout is less: I increased it to 100 seconds, but still keep getting this error.
- Memory leak: I keep on getting
GC
warnings. I've read articles and tried to improve my code but it doesn't help either. I must also mention that my app downloads 2000+ 30KB JSON files one after other in a background thread. Suggestions to improvise this efficiently would be highly welcomed ! - Server issues: Since Amazon Web Service is highly reliable, it might not be the underlying problem.
- Multiple threading: Could this be responsible somehow ?
- Erroneous way of downloading: I doubt if I'm downloading in an inefficient way. Correct me if I'm wrong.
Please help me figure out the real issue. Thanks !
public synchronized String getJSONString(String url)
{
try {
URL url1 = new URL(url);
URLConnection tc = url1.openConnection();
tc.setConnectTimeout(timeout);
tc.setReadTimeout(timeout);
// tc.connect();
br = new BufferedReader((new InputStreamReader(tc.getInputStream())),8000);
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
json = sb.toString();
return json;
}
catch(Exception e)
{
Log.e("JSON Downloader", "Error downloading feed/article ");
e.printStackTrace();
}
return null;
}
Error log:
02-01 06:37:43.375: W/System.err(5548): java.net.SocketTimeoutException
02-01 06:37:43.375: W/System.err(5548): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
02-01 06:37:43.375: W/System.err(5548): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-01 06:37:43.375: W/System.err(5548): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-01 06:37:43.375: W/System.err(5548): at java.io.InputStream.read(InputStream.java:163)
02-01 06:37:43.375: W/System.err(5548): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
02-01 06:37:43.375: W/System.err(5548): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
02-01 06:37:43.375: W/System.err(5548): at libcore.io.Streams.readAsciiLine(Streams.java:201)
02-01 06:37:43.375: W/System.err(5548): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
02-01 06:37:43.375: W/System.err(5548): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
02-01 06:37:43.375: W/System.err(5548): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
02-01 06:37:43.375: W/System.err(5548): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-01 06:37:43.375: W/System.err(5548): at com.in.feeds.JSONDownloader.getJSONString(JSONDownloader.java:65)
02-01 06:37:43.375: W/System.err(5548): at com.in.feeds.JSONDownloader.getJSONObjectFromUrl(JSONDownloader.java:45)
02-01 06:37:43.375: W/System.err(5548): at com.in.fullarticle.ArticlePage$LoadArticle.run(ArticlePage.java:383)
02-01 06:37:43.375: W/System.err(5548): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-01 06:37:43.375: W/System.err(5548): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-01 06:37:43.375: W/System.err(5548): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-01 06:37:43.375: W/System.err(5548): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-01 06:37:43.375: W/System.err(5548): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-01 06:37:43.375: W/System.err(5548): at java.lang.Thread.run(Thread.java:856)