2011-10-17 64 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秒 –