简体   繁体   English

Android HttpURLConnection非常慢

[英]Android HttpURLConnection VERY slow

This is the situation I'm facing with the code below: 这是我下面的代码所面临的情况:

As you can see I'm trying to read an HTTP stream. 如您所见,我正在尝试读取HTTP流。 When I run the following code on the Android simulator it works 100% of the time, when I run the following code on my Galaxy S3 while on 3G it works 100% of the time, when I try to connect to the URL using my laptop browser it works 100% of the time, when I try to connect using the Galaxy S3 browser (in both wifi and 3g) it works... 100% of the time. 当我在Android模拟器上运行以下代码时,它可以100%的时间运行;当我在Galaxy S3上运行以下代码时,在3G上,当我尝试使用笔记本电脑连接到URL时,它的运行时间可以达到100%。浏览器它可以100%地工作,当我尝试使用Galaxy S3浏览器(在wifi和3g中)连接时,它可以... 100%的时间。 HOWEVER, when I try to connect using my Galaxy S3 while on Wi-Fi I time out ~80% of the time. 但是,当我尝试在Wi-Fi上使用Galaxy S3连接时,我的超时时间约为80%。 If I remove the timeout properties I get weird exceptions such as: 如果删除超时属性,则会出现奇怪的异常,例如:

"recvfrom failed: ETIMEDOUT"
"failed to connect to <URL>: connect failed: ENETUNREACH (Network is unreachable)"
"unable to resolve the host <URL>: no address associated with hostname"

I'm open to any suggestions... 我愿意接受任何建议...

public static final String getHttpResponse(String url)
{
    HttpURLConnection conn = null;
    InputStream response = null;
    try { 
        URL address = new URL(url);
        conn = (HttpURLConnection)address.openConnection();
        conn.setConnectTimeout(30 * 1000); //30 seconds
        conn.setReadTimeout(30 * 1000); //30 seconds

        response = conn.getInputStream();

        if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) { 
            Log.e("Util.getHttpResponse", Integer.toString(conn.getResponseCode()));
            return null; 
        }

        String result = Util.streamToString(response);
        return result;

    } catch(IOException e) {
        response = conn.getErrorStream();
        Log.e("Util.getHttpResponse", Util.streamToString(response));
        return null;

    } finally { 
        if( response != null ) { 
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn != null) { 
            conn.disconnect();
        }
    }
}

UPDATE: - using AndroidHttpClient did not work - After getting the input stream I had an error popup right in the eclipse IDE... As you can see my debug cursor made it all the way to line 107.. well after I was done getting the input stream this time... 更新:-使用AndroidHttpClient不起作用-在获取输入流后,我在eclipse IDE中出现了一个错误弹出窗口...如您所见,我的调试光标一直到达第107行。这次输入流... 日食

I got the same problem on Android device. 我在Android设备上遇到了同样的问题。 I use an IP address in the url. 我在网址中使用IP地址。 Final, I found the HttpURLConnection.connect(...) method involved the getHostName() internally. 最终,我发现HttpURLConnection.connect(...)方法在内部涉及getHostName()。 After that, I use the domain in the url, then it works 100% of the time. 之后,我在url中使用域,那么它在100%的时间内都有效。

As an experiment, what if you try using AndroidHttpClient class instead for doing the same? 作为实验,如果尝试使用AndroidHttpClient类而不是这样做怎么办? It has some predefined timeouts and other settings which, we were told, should work fine in most cases. 它具有一些预定义的超时和其他设置,我们被告知,在大多数情况下应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM