public class HtmlActivity extends Activity implements OnClickListener {    private static final int SUCCESS = 0;    private static final int FAILURE = 1;    private static final int ERROR = 2;    private Button btnView;    private TextView tvContent;    private EditText etUrl;    private Handler mHandler = new Handler() {        public void handleMessage(Message msg) {            switch (msg.what) {            case SUCCESS:                String content = (String) msg.obj;                tvContent.setText(content);                break;            case FAILURE:                break;            case ERROR:                break;            default:                break;            }        };    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_html);        initView();    }    private void initView() {        btnView = (Button) findViewById(R.id.btn_html);        tvContent = (TextView) findViewById(R.id.tv_content);        etUrl = (EditText) findViewById(R.id.et_url);        btnView.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if (v == btnView) {            try {                URL url = new URL(etUrl.getText().toString());                HttpURLConnection conn = (HttpURLConnection) url.openConnection();                conn.setRequestMethod("get");                conn.setConnectTimeout(5000);                conn.setDoInput(true);                conn.setDoOutput(true);                conn.setUseCaches(false);                // conn.setRequestProperty("", newValue);                conn.connect();                if (conn.getResponseCode() == 200) {                    String result = NetUtils.parseStream(conn.getInputStream());                    Message msg = Message.obtain();                    msg.what = SUCCESS;                    msg.obj = result;                    mHandler.sendMessage(msg);                } else {                    mHandler.sendEmptyMessage(FAILURE);                }            } catch (Exception e) {                e.printStackTrace();                mHandler.sendEmptyMessage(ERROR);            }        }    }}
public class NetUtils {    public static String parseStream(InputStream inputStream) {        ByteArrayOutputStream bos = new ByteArrayOutputStream();        try {            byte[] buffer = new byte[1024];            int len = -1;            while((len = inputStream.read(buffer))!=-1){                bos.write(buffer, 0, len);            }            inputStream.close();            bos.close();        } catch (IOException e) {            e.printStackTrace();            return "获取失败";        }        try {            return new String(bos.toByteArray(),"utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return null;    }}

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android(安卓)handler异步更新
  3. android获取指定路径下目录文件
  4. ANDROID截屏
  5. Android(安卓)基于GeolocationAPI的基站定位
  6. Android(安卓)通过短链接,获取长连接
  7. android获取mac地址
  8. 获取Android唯一设备号
  9. Android(安卓)之获取图片及视频缩略图

随机推荐

  1. Android开发网上的一些重要知识点
  2. android画了一个笑脸
  3. Android获取IMEI号
  4. android 字符串转json
  5. 获取android联系人信息
  6. android机顶盒获取有线mac
  7. Android多次加载bitmap后,提示内存溢出。
  8. android 反射解析xml文件成为java对象
  9. How to build Android(安卓)adb for ARM
  10. Android(安卓)Canvas学习笔记(1)