2011-10-17 68 views
1

我使用下面的代码从http服务器检索一些文本。尺寸小于1 kB并在0.002毫秒内生成Android的url.openStream慢?

但是,检索数据可能需要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之间。当使用铬我可以加载the page在300-350毫秒。我想知道是否有更高效的方法来检索此数据

回答

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(); 
+0

它比以前更好,使用下面的脚本: http://androidforums.com/application-development/9261-doing-http-request-android。 html但是:第一个请求的响应时间仍然需要1600毫秒,而下一个请求的响应时间为+/- 700秒 –