android.os.NetworkOnMain ThreadException

 余温
2018年05月18日 00时53分
 android


先创建子线程

new Thread() {
//要执行的代码写在这个里面
 }.start();

就像这样  //这是一个获取页面源码的方法

public void click(View V) {
    //创建子线程
    new Thread() { 
        public void run() {
            try {
                String path = et_path.getText().toString().trim();//表单输入地址
                URL url = new URL(path);//要请求的地址
                //实例化对象
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                //发送请求
                conn.setRequestMethod("GET");
                //请求超时
                conn.setConnectTimeout(5000);
                //获取状态
                int code = conn.getResponseCode();
                if (code == 200) {
                    //获取到的文件流数据
                    InputStream in = conn.getInputStream();

                    String content = getInputStream.readStream(in);
                    //在子线程更新ui

                    Message msg = new Message();
                    msg.obj = content;
                    handler.sendMessage(msg);//发了一个消息  执行上面的重写方法 handleMessage

                    //tv_reuslt.setText(content);
                } else {
                    tv_reuslt.setText("失败");
                }
            } catch (Exception e) {
                System.out.println("错误!");
                e.printStackTrace();
                System.out.println("------------------------------------------!");

            }
        }
    }.start();
}

// 这个是把文件流转换成字符串

String content = getInputStream.readStream(in);
public class getInputStream {
    public static String readStream(InputStream in) throws Exception{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) != -1){
            baos.write(buffer,0,len);
        }
        in.close();
        String content = new String(baos.toByteArray());
        return content;
    }

//handler  回到主线程更新ui

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        //在这里更新ui  主线程
        String text = (String) msg.obj;
        tv_reuslt.setText(text);
    }
};

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/et_path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入网址"/>
   <Button
       android:onClick="click"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="查看"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="哈哈"/>
    </ScrollView>
</LinearLayout>


{{vo.nickname}}:{{vo.content}}

{{vo.time}} 回复


  • {{level.nickname}} 回复 {{level.father_nickname}}{{level.content}}
  • {{level.time}} 回复


@
登陆后评论