0

我想从网上获取 html,但我不能。这是我的代码

final EditText et = (EditText) findViewById(R.id.editText1);
    final TextView tv = (TextView) findViewById(R.id.textView1);
    final Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            try {
                URL url = null;
                url = new URL(et.getText().toString());
                URLConnection conn = url.openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    tv.append(line);

                }
            } catch (Exception e) {
                Log.e("error", "erorr connection "+ e.toString());
            }               
        }
    });

我也已经添加了来自互联网的许可。目标版本 4.0.3

4

2 回答 2

3

// 创建 HTTP 客户端

HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet("http:// yoururl"); 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) 
        sb.append(line + "\n");

    String resString = sb.toString(); // Result is here

    is.close(); // Close the stream
于 2012-09-07T06:12:01.197 回答
1

请将网络部分放入后台线程,然后更新到前台 UI 线程。您可以使用 AsyncTask 来执行此操作。

于 2012-09-07T06:28:47.973 回答