Android使用获取HTML


 1 public byte[] readStream(InputStream inputStream) throws Exception 
 2     {
 3         byte[] buffer = new byte[1024];
 4         int len = -1;
 5         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 6 
 7         while ((len = inputStream.read(buffer)) != -1) 
 8         {
 9             byteArrayOutputStream.write(buffer, 0, len);
10         }
11 
12         inputStream.close();
13         byteArrayOutputStream.close();
14         return byteArrayOutputStream.toByteArray();
15     }
16 
17     public String testGetHtml(String urlpath) throws Exception 
18     {
19         URL url = new URL(urlpath);
20         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
21         conn.setConnectTimeout(1000);//超时时间1秒钟
22         conn.setRequestMethod("GET");
23 
24         if (conn.getResponseCode() == 200) 
25         {
26             InputStream inputStream = conn.getInputStream();
27             byte[] data = readStream(inputStream);
28             String html = new String(data);
29             return html;
30         }
31         return "bb";
32     }

 

如果在android4.0以下的版本中,可以直接使用。否则,这部分网络编程就需要使用到多线程,如果不使用多线程就会抛出android.os.NetworkOnMainThreadException异常。下面是多线程的代码

 1 private static final int MSG_SUCCESS = 0;// 获取成功的标识
 2 private static final int MSG_FAILURE = 1;// 获取失败的标识
 3 
 4 protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7         tx = (TextView) findViewById(R.id.tx);
 8         html = (Button) findViewById(R.id.Button1);
 9         html.setOnClickListener(new View.OnClickListener(){
10             @Override
11             public void onClick(View v) {
12                 new Thread(){
13                     public void run(){
14                         try {
15                             String testStr="aa";
16                             testStr = testGetHtml("http://www.baidu.com/");
17                             mHandler.obtainMessage(MSG_SUCCESS, testStr).sendToTarget();
18                         } catch (Exception e) {
19                             e.printStackTrace();
20                             StringWriter sw = new StringWriter();  
21                             e.printStackTrace(new PrintWriter(sw, true));  
22                             String str = sw.toString();  
23                             mHandler.obtainMessage(MSG_FAILURE, str).sendToTarget();
24                         }
25                     }
26                 }.start();
27             }
28         });
29         
30         
31         //HttpGetData();
32     }
33     private Handler mHandler = new Handler(){
34         public void handleMessage(Message msg)
35         {
36             switch(msg.what)
37             {
38                 case MSG_SUCCESS:
39                     tx.setText(msg.obj.toString());break;
40                 case MSG_FAILURE:
41                     tx.setText(msg.obj.toString());break;
42             }
43         }
44     };

 

posted @ 2014-04-28 16:25  胡小六  阅读(508)  评论(0编辑  收藏  举报