【问题标题】:Timeout when downloading a string下载字符串时超时
【发布时间】:2011-10-13 20:38:20
【问题描述】:

我正在使用以下两个函数从服务器下载字符串。我还记录了下载文本所需的时间,无论是客户端看到的还是服务器看到的。下载的字符串永远不会一样。

服务器时间只有几毫秒,但客户端看到的时间平均为 100 毫秒,具体取决于 wifi 信号。即使服务器时间仍在可接受的范围内,客户端时间有时也会上升到 3000 毫秒(但从不超过 3200 毫秒)。

我开始认为超时是在某个地方定义的,但我不知道它可能在哪里。它不在我的代码中,我在开发者网站和谷歌上环顾四周没有结果。

我希望有人能给我一些线索来定义这个延迟,并确认它默认是 3000 毫秒。

private String DownloadText(String URL)
{
    String str = "";
    int BUFFER_SIZE = 2000;
    InputStream in = null;
    try{
        in = OpenHttpConnection(URL);
    } catch (IOException e1) {
        e1.printStackTrace();
        return "";
    }
    catch(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;        
}

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;     
}

顺便说一句:我从谷歌的一个搜索结果中借用了这两个功能。

编辑:我从一个线程中调用 DownloadText(url)。我开始认为这可能与超时有关。是吗?

【问题讨论】:

    标签: android http download timeout


    【解决方案1】:

    这将对您有所帮助:

      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);
      ....
    

    【讨论】:

    • 这不能回答我的问题。我不想自己定义,而是找出超时的原因。
    【解决方案2】:

    我以前见过类似的行为。就我而言,它是在 AJAX 调用中,这对我来说也是一个真正的难题。事实证明,在我的情况下,服务器正在返回数据而没有任何一个

    1. 指定内容长度或
    2. 关闭 http 连接

    因此浏览器必须等待连接超时才能处理数据并生成接收事件。您的情况可能会发生类似的情况,因此请打开网络分析软件并验证 http 的正确性。我通常使用 Fiddler2 来完成这种类型的工作,但我不知道你是否可以让 android 设备很好地通过代理。听起来你控制着网络服务器,所以也许可以从那端检查 tcp 数据包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多