问题描述
我可以成功地连接,发送和使用的HttpURLConnection接收数据。但是这需要很长的时间来加载在我的手机(三星S4,4.2),并在Android 4.2模拟器的所有数据。但它需要近1-2秒(这是非常快的)加载在Android 2.3.x模拟器图片。比我的HTTP连接星系S4快。
I can succesfully connect, send and receive data by using httpurlconnection. But it takes very long time to load all the data on my phone (Samsung s4, 4.2) and on android 4.2 emulator.But it takes almost 1-2 seconds (which is very fast) to load pics on Android 2.3.x emulator. Faster than my galaxy s4 on http connection.
我使用的AsyncTask和我的code正常工作的两个。这是在Android 4.2s只是缓慢。我试图消除chunkedStreaming,保活,改变超时值等,但仍然没有成功。
I'm using AsyncTask and my code works fine on both. It is just slow on android 4.2s.I tried removing chunkedStreaming, keep alive, changing timeout values etc. but still no success
下面是我的code
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setConnectTimeout(6000);
urlConnection.setReadTimeout(6000);
urlConnection.setRequestProperty("Content-Type", "multipart/form-data;charset=UTF-8;boundary="+boundary);
urlConnection.connect();
是否有与4.2的和2.3.x的httpurlconnections什么区别吗?请告诉我错在这里
Are there any differences between 4.2's and 2.3.x's httpurlconnections? Whats wrong here
更新!
我测试用Log.e(),看看哪条线路需要大量的时间。
I tested by using Log.e() to see which line takes most time.
///// other staff
////......
Log.e("HTTP","3");
if (isCancelled())
return (null); // don't forget to terminate this method
Log.e("HTTP","3");
//Output
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
//Send Passcode
Log.e("HTTP","4");
3和4之间,5-6秒传递就行
Between 3 and 4, 5-6 second passes on the line
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
更新!!
这等待的时间(见previous更新)中相关urlConnection.setConnectTimeout(6000);
That waiting time (see previous update) is related to the urlConnection.setConnectTimeout(6000);
当我做超时1000,然后连接响应快(等待1秒就行)
When I make Timeout 1000, then connection responses quickly (waiting 1 second for the line)
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
不知道为什么会这样
No idea why this is happening
推荐答案
设置urlConnection.setConnectTimeout(),以较低的超时。
Set urlConnection.setConnectTimeout() to a lower timeout.
类文档URLConnection.setConnectTimeout()说:
The class documentation for URLConnection.setConnectTimeout() says:
设定的最长时间,以毫秒为单位,而连接等。如果超时时间建立连接之前,连接到服务器会失败,一个SocketTimeoutException如果。 0默认值会导致我们做一个阻塞连接。这并不意味着我们将永远不会超时,但它可能意味着几分钟后,你会得到一个TCP超时。
警告:如果主机名解析为多个IP地址,此 客户端将尝试每个在RFC 3484的顺序。如果连接到每个这些 地址失败,多次超时将之前的连接经过 尝试抛出一个异常。同时支持IPv6和主机名 IPv4的始终至少有2个IP地址。
Warning: if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.
我本来有我设置为 urlConnection.setConnectTimeout(30000);
键,然后将其更改为 urlConnection.setConnectTimeout(1000)
。随即,我看到了更快的结果。
I originally had mine set to urlConnection.setConnectTimeout(30000);
and then changed it to urlConnection.setConnectTimeout(1000)
. Immediately, I saw quicker results.
希望这有助于!
这篇关于HttpURLConnection类是在Android 4.2很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!