我使用以下代码从http服务器检索一些文本。大小小于1 kB并且以0.002毫秒
生成但是,检索数据可能需要600毫秒,但大多数在2000到5000毫秒之间。
使用以下代码:
long starttime = System.currentTimeMillis();
StringBuffer SB = new StringBuffer();
Common.toLog("101 took "+ (System.currentTimeMillis() - starttime) + " ms");
try {
URL url = new URL(Common.server+request);
Common.toLog("102 took "+ (System.currentTimeMillis() - starttime) + " ms");
InputStreamReader ISR = new InputStreamReader(url.openStream());
Common.toLog("102a took "+ (System.currentTimeMillis() - starttime) + " ms");
BufferedReader in = new BufferedReader(ISR);
Common.toLog("103 took "+ (System.currentTimeMillis() - starttime) + " ms");
String inputLine;
while ((inputLine = in.readLine()) != null) {
SB.append(inputLine);
}
in.close();
Common.toLog("105 took "+ (System.currentTimeMillis() - starttime) + " ms");
} catch (IOException e)
{
Common.toLog("Could not make connection 1");
showMSG(R.string.ERROR_NO_INTERNET, true);
}
最耗时的方法是在对数点102和点102a之间。使用chrome时,我可以在300-350毫秒内加载the page。我想知道是否有更有效的方法来检索这些数据
答案 0 :(得分:0)
您撤回的数据并不是那么久。不要打开URL的读取流,而是尝试使用HttpRequest的一个实现:
http://developer.android.com/reference/org/apache/http/HttpRequest.html
这样,就会发出一个请求,您的数据会在响应中返回。例如,您可以尝试HttpGet请求:
HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();