从HTML文件中读取Android

时间:2012-08-22 23:59:31

标签: android html nullpointerexception

我正在尝试从url源读取一个html文件,但是我得到了一个nullpointerexception。 这是我的代码:

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);

2 个答案:

答案 0 :(得分:1)

您尚未初始化StringBuffer对象:

StringBuffer sb = new StringBuffer();

您也可以尝试使用此代码来阅读来自网址的内容:

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" });

  }
} 

答案 1 :(得分:0)

  

LogCat说NullPointerException来自sb.append(htmlLine);

sb为null,因为您从未初始化它。尝试添加:

StringBuffer sb = new StringBuffer();