1

我正在尝试从 url 源读取 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); .

4

2 回答 2

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

  }
} 
于 2012-08-23T00:07:02.880 回答
0

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

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

StringBuffer sb = new StringBuffer();
于 2012-08-23T00:03:12.430 回答