升级到android 2.3.5和2.3.6后,android的代码 HttpURLConnection类:urlconn.connect(),urlconn.getOutputStream() 并且urlconn.getInputStream()变得非常慢(每个命令都需要 超过5秒,在版本2.3.2或2.2下它只需要少于 1秒)。 有没有人有相同的情况?
我的代码如下:
getUrl = new URL(url);
urlConn = (HttpURLConnection) getUrl.openConnection();
urlConn.setUseCaches(false);
urlConn.setRequestMethod(httpMethod.name());
urlConn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
urlConn.setReadTimeout(HTTP_READ_TIMEOUT);
urlConn.setDoInput(true);
if (requestBody != null)
urlConn.setFixedLengthStreamingMode(requestBody.length());
if (!(httpMethod == HttpMethod.GET))
urlConn.setDoOutput(true);
urlConn.connect();
if (requestBody != null) {
osw = new OutputStreamWriter(urlConn.getOutputStream());
osw.write(requestBody);
osw.flush();
}
in = urlConn.getInputStream();
buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[256];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
responseBody = buffer.toByteArray();
responseCode = urlConn.getResponseCode();
此致 征
答案 0 :(得分:0)
两点建议:
答案 1 :(得分:0)
尝试在单独的线程上运行它。