2012-08-22 114 views
1

我想從url源讀取html文件,但我得到了nullpointerexception。 這裏是我的代碼:Android從HTML文件讀取

public class NetworkThread extends AsyncTask<String, Integer, String> { 

    URL url; 
    StringBuffer sb; 
    InputStream is; 


    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     String webUrl = etUrl.getText().toString(); 
     try { 
      url = new URL(webUrl); 
      BufferedReader in = new BufferedReader(
      new InputStreamReader(url.openStream())); 
      String htmlLine; 
      while ((htmlLine = in.readLine()) != null) 
       sb.append(htmlLine); 

      in.close(); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return "Bad URL"; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return "Input/Output"; 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
      return "Null"; 
     } 
     return sb.toString(); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     html.setText(result); 
    } 

} 

我試着在普通的Java運行這個程序 - 硬編碼的URL,它運行良好。

但是,當我在android上運行它時,我在TextView中得到了「Null」消息。

LogCat說NullPointerException是從sb.append(htmlLine);

+0

這裏是'聲明etUrl'? – zizoujab

回答

1

您尚未初始化StringBuffer對象:

StringBuffer sb = new StringBuffer(); 

你也可以試試這個代碼來讀取URL內容:

public class ReadWebpageAsyncTask extends Activity { 
    private TextView textView; 


/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView = (TextView) findViewById(R.id.TextView01); 
    } 

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 
     for (String url : urls) { 
     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     try { 
      HttpResponse execute = client.execute(httpGet); 
      InputStream content = execute.getEntity().getContent(); 

      BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) { 
      response += s; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     textView.setText(result); 
    } 
    } 

    public void readWebpage(View view) { 
    DownloadWebPageTask task = new DownloadWebPageTask(); 
    task.execute(new String[] { "http://www.stackoverflow.com" }); 

    } 
} 
+2

是的,我以爲這是一個StringBuilder,但是它是一個StringBuffer。 – Sam

+0

感謝它現在的作品,HTML加載。 – Denizen

+0

@Denizen歡迎您不要忘記註冊或接受答案 –

0

LogCat說NullPointerException是從sb.append(htmlLine);

sb爲空,因爲您從不初始化它。嘗試添加:

StringBuffer sb = new StringBuffer();