2013-05-07 48 views
0

我有一個Android應用程序,我從多個URL中提取數據,然後保存爲字符串arraylist。它工作正常,但從13個網址獲取數據需要15​​-20秒。從同一套網址獲取數據的地方在使用phonegap構建的相同應用程序中需要3-4秒。這是下面的代碼。從服務器提取數據需要太多時間 - android

 @Override 
     protected String doInBackground(String... params) { 
      client = new DefaultHttpClient(); 
      for(int i=0;i<url.size();i++) 
      { 
       get = new HttpGet(url.get(i)); 
       try { 
        response = client.execute(get); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       entity = response.getEntity(); 
       InputStream is = null; 
       try { 
        is = entity.getContent(); 
       } catch (IllegalStateException e) {   
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is)); 
       StringBuffer buffer = new StringBuffer(); 
       String line = null; 
       do { 
        try { 
         line = reader.readLine(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        buffer.append(line); 
       } while (line != null); 
       String str = buffer.toString(); 
       param.add(str);    
      } 
      return null; 
     } 

任何人都可以請建議我如何加快執行速度並縮短提取時間。

+0

你可以做並行執行...使用HttpURLConnection的類 – Prachi 2013-05-07 09:15:41

+0

什麼是Android的API級別您的應用程序上運行? – TactMayers 2013-05-07 09:16:00

+0

@tactmayers,使用14! – bharath 2013-05-07 09:21:50

回答

0

您可以嘗試爲for循環的每次迭代啓動一個單獨的線程。 水木清華這樣的:

for(int i = 0; i < url.size(); i++){ 
    //start thread that gets data from url and adds it to the list 
}