开发者

Timeout when downloading a string

I am using the following two functions to download a string from a server. I am also logging the time it takes to download the text, both as seen by the client and also as seen by the server. The downloaded string is never the same.

The server time is only few milliseconds but the time seen by the client is on average 100 milliseconds depending on the wifi signal. Occasionally the client time goes up to 3000 milliseconds (but never higher than 3200 ms) even though the server time is still within acceptable limits.

I'm starting to think that a timeout is somewhere defined but I don't know where it might be. It´s not in my code and I've looked around on the developer site and google without results.

I'm hoping that someone can give me some clues where this delay might be defined and confirm that it is 3000 ms by default.

private String DownloadText(String URL)
{
    String str = "";
    int BUFFER_SIZE = 2000;
    InputStream in = null;
    try{
        in = OpenHttpConnection(URL);
    } catch (IOException e1) {
        e1.printStackTrace();
        return "";
    }
    ca开发者_StackOverflow社区tch(ArithmeticException ae){
        //
    }
    try{
        InputStreamReader isr = new InputStreamReader(in);
        int charRead;

          char[] inputBuffer = new char[BUFFER_SIZE];          
        try {
            while ((charRead = isr.read(inputBuffer))>0)
            {                    
                //---convert the chars to a String---
                String readString = 
                    String.copyValueOf(inputBuffer, 0, charRead);                    
                str += readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;        
}

with

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }
    }
    catch (Exception ex) {
        throw new IOException("Error connecting");            
    }
    return in;     
}

BTW: I borrowed the two functions from one of google's search results.

EDIT: I am calling DownloadText(url) from within a thread. I was beginning to think that could have something to do with the timeout. Does it ?


This will help you:

  private static final int CONNECT_TIMEOUT_MILL = 10000;
  private static final int READ_TIMEOUT_MILL = 3000;
  ....
  HttpURLConnection con = (HttpURLConnection) url.openConnection();
  con.setConnectTimeout(CONNECT_TIMEOUT_MILL);
  con.setReadTimeout(READ_TIMEOUT_MILL);
  ....


I've seen similar behavior like this before. In my case it was in AJAX calls and it was a real head puzzler for me too. It turned out in my case that the server was returning the data without either

  1. specifying the content length or
  2. closing the http connection

So the browser had to wait for the connection to timeout before it would process the data and generate the receive event. Something similar might be happening in your case, so break out the network analysis software and verify the http correctness. I normally use Fiddler2 for this type of work, but I don't know if you can make the android device go through a proxy very well. It sounds like you control the web server, so maybe inspecting the tcp packets from that end is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜