package com.example.ceshi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;d

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class get extends Activity {
    // textview
    private TextView mTextView;
    // handler线程
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mTextView.setText((String) msg.obj);
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get);
        // 绑定id
        mTextView = (TextView) findViewById(R.id.a);

    }

    // 联网
    private void getHttp() {
        InputStream stream = null;
        InputStreamReader re = null;
        BufferedReader reader = null;
        try {
            // 找水源
            URL url = new URL("http://www.taobao.com");
            // 建立总闸
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            // 建立水道
            stream = conn.getInputStream();
            // 捕鱼
            // 小渔网
            re = new InputStreamReader(stream);
            // 大渔网
            reader = new BufferedReader(re);

            // 捞鱼
            // 大水桶
            String len = "";
            // 小水桶
            String temp = "";
            while ((temp = reader.readLine()) != null) {
                len += temp;
            }
            // 更新UI
            Message sg = new Message();
            sg.obj = len;
            mHandler.sendMessage(sg);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 晒网,关闭资源
            try {
                if (reader != null) {
                    reader.close();
                }
                if (re != null) {
                    re.close();
                }
                if (stream != null) {
                    stream.close();
                }

            } catch (Exception e2) {
            }
        }
    }

    // 按钮监听
    public void aaa(View v) {
        //新建一个线程
        new Thread() {
            public void run() {
                getHttp();
            };
        }.start();
    }
}
