2013-03-17 196 views
0

我有一些錯誤,當我讀主線程獲取網頁內容

public class WebReader extends AsyncTask<String, String, String> { 

    private String result; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String result; 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(params[0]); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     System.out.println(params[0]); 
     try { 
      result = client.execute(request, responseHandler); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     this.result = result; 
     return result; 

    } 

    protected void onProgressUpdate(String... progress) { 

    } 

    @Override 
    protected void onPostExecute(String result) { 
    this.result = result; 
} 

    public String getResult(){ 
     return result; 
    } 

} 

上的網頁內容,而且我用這個在:

public static void testAsync(){ 
    String result = null; 
    WebReader wr = new WebReader(); 
    wr.execute("http://www.google.fr"); 
    result = wr.getResult(); 
    while(result==null){ 
     try { 
      Thread.sleep(1000); 
      result = wr.getResult(); 
     } catch (InterruptedException e) { 
      result = ""; 
     } 

    } 
    System.out.println(result); 
} 

我並不真的找到soltion返回一個字符串

感謝

回答

0

我認爲這個問題是行

this.result = result; 

protected void onPostExecute(String result) { 

,因爲我沒有看到onPostExecute如何被傳遞的結果參數,所以你要分配NULL。我刪除了該行,它工作。