2014-05-22 29 views
0

我使用AsyncTask來下載網頁源代碼(HTML)。它工作正常,但需要很長時間才能執行..有沒有更好的方法。我認爲這是在UrlConnection中浪費時間。我試圖用HttpClient做到這一點,但無法獲得Html源代碼。幫助我使它快速或..告訴我如何把這個AsyncTask線程?在android中下載HTML的快速方法?

class RetrieveFeedTask extends AsyncTask<String, Void, String> 
{ 
    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 

     Utilities.hideSoftKeyboard(PlayListActivity.this); 

     progressDialog = ProgressDialog.show(PlayListActivity.this, 
       "Loading...", "Please wait..."); 
    } 

    @Override 
    protected String doInBackground(String... urls) { 
     try { 
      URL url= new URL(urls[0]); 

      URLConnection conn = url.openConnection(); 

      // open the stream and put it into BufferedReader 
      BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream())); 

      String inputLine; 
      String HTML_response = ""; 
      while ((inputLine = br.readLine()) != null) { 
       // System.out.println(inputLine); 
       HTML_response += inputLine; 
      } 
      br.close(); 

      System.out.println("Done"); 
          Parser(feed); 

      return HTML_response; 
     } /*catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }*/catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(String feed) 
    { 
     super.onPostExecute(feed); 

     PlayListAdapter adapter = new PlayListAdapter(
      PlayListActivity.this, list); 

    list_of_songs.setAdapter(adapter); 

     progressDialog.dismiss(); 
    } 
} 

@SuppressLint("NewApi") 
void Parser(String x) 
{ 
    if (x.contains(spliter_start)) 
    { 
     if (spliter_end.isEmpty()) 
     { 
      x = x.substring(x.indexOf(spliter_start)); 
     } 
     else 
     { 
      x = x.substring(x.indexOf(spliter_start), 
        x.indexOf(spliter_end)); 
     } 

    } 

    int i = 0; 
    list.clear(); 
    while (x.contains(loop_controller)) 
    { 
     if (i > 50) 
     { 
      break; 
     } 

     HashMap<String, String> map = new HashMap<String, String>(); 

     x = x.substring(x.indexOf(song_start)); 


     map.put("songsName",x.substring(
       x.indexOf(song_start) + song_start.length(), 
       x.indexOf(song_end)));// songsName.get(i)); 

     x = x.substring(x.indexOf(song_url_start)); 

     map.put("songsUrl",x.substring(
       x.indexOf(song_url_start) + song_url_start.length(), 
       x.indexOf(song_url_end)));// songsUrl.get(i)); 


     list.add(map); 

     i++; 
    } 



} 
+0

'AsyncTask'已經在'doInBackground()'中完成的工作使用後臺線程。使用Traceview來確定你的性能問題。最有可能的是,你需要將你的'Parser'工作移到'doInBackground()'中。 – CommonsWare

+0

我已經嘗試過了,但是沒有我需要的那麼快?使用AsyncTask更好嗎 – Nepster

+0

「使用Traceview確定性能問題的位置」是什麼部分你不明白嗎? – CommonsWare

回答

0

首先,AsyncTasks已經在一個單獨的線程中運行,所以你不必擔心這裏。其次,所有的AsyncTasks都必須在doInBackground方法中執行它們的長時間操作,因爲在這裏線程運行,所以你的問題在於你在onPostExecute方法中調用你的解析器。你可以將其更改爲這個(假設你的對象list是一個全局變量:

@Override 
protected String doInBackground(String... urls) { 
    (...) 

     System.out.println("Done"); 

     return Parser(HTML_response); 
    (..) 
} 

@Override 
protected void onPostExecute(Void void) 
{ 
    super.onPostExecute(feed); 

    PlayListAdapter adapter = new PlayListAdapter(
     PlayListActivity.this, list); 

    list_of_songs.setAdapter(adapter); 

    progressDialog.dismiss(); 
} 
} 

關於下載的代碼,我認爲這是確定,我真的不知道,如果它可以更高效

希望幫助,問候

+0

我已經試過了,也更新了我的問題..根本沒有效果。 – Nepster

0

這一部分:

String inputLine; 
    String HTML_response = ""; 
    while ((inputLine = br.readLine()) != null) { 
     // System.out.println(inputLine); 
     HTML_response += inputLine; 
    } 

是瘋了 - 你永遠不應該做這樣的事情,與字符串作爲每一個所迭代!循環new String創建。用戶StringBuilder.append();而不是String HTML_response