SocketTimeoutException Android
Asked Answered
W

1

9

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)
Windfall answered 1/2, 2013 at 7:48 Comment(0)
A
1

it is a while I worked with this, but one article is really recommended to read when looking for performance: http://www.kegel.com/java/wp-javaio.html

The connection timeout could be caused on the server side, assuming you are connected to a web server, check what errors you receive there.

The GC statements are not surprising. This is not a memory leak, but Java cleaning up. From above article:

First, if we look at the first line of the while loop, we see that a new String object is being created for every line of the file being read:

while ((line = in.readLine()) != null) {

This means, for example, that for a 100,000 line file 100,000 String objects would be created. Creating a large number of objects incurs costs in three ways: Time and memory to allocate the space for the objects, time to initialize the objects, time to garbage collect the objects.

Regarding multiple threading, you should provide a bit more code. Your method is synchronized, so you at least avoid multiple invocations on the same instance at the same time. The NW code is, at quick glance, safe.

My debug strategy would be to look at the server side first and second store timestamps on when you receive a line of input to see if there are gaps occurring (transmission errors).

Good luck

Assize answered 4/2, 2013 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.