一、网页源码查看器

1.shift+F2调用httpwatch(为网页抓包数据分析工具【需下载】):

2.HttpURLConnection:用于发送和接收数据

3.必须有联网权限:android.permission.INTERNET

4.异常处理方式:

//若出现此种异常,
W/System.err(5504): android.os.NetworkOnMainThreadException
W/System.err(5504): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)

//在oncreate方法中加入
 StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

5.ScrollView(滚动条):使用注意:只能有一个孩子

二、消息机制的写法

1.如果在主线程中进行了耗时的操作(比如连接网络,拷贝数大的数据)。

2.避免出现anr异常可将耗时操作放置子线程中,自己创建一个线程。

3.4.0之后谷歌强制要求连接网络不能再主线程进行访问

4.只有主线程才可以更新ui

三、Handler原理

handler使用步骤:

1.在主线程定义一个Handler:private Handler handler = new Handler();

2.使用handler会重写Handler里面的handleMessage方法:public void handleMessage(android.os.Message msg) {};

3.拿着我们在主线程创建的Handler 去子线程发消息:handler.sendMessage(msg); 

4.handleMessage方法就会执行,在这个方法里面去更新ui

handler作用:用来发消息和处理消息

looper作用:是去消息队列里面取消息,Looper是在主线程一创建,looper就有了。

 网站源码查看器示例代码:

public class MainActivity extends Activity {

    protected static final int REQUESTSUCCESS = 0;  //变大写ctrl+shift+X   变小写写ctrl+shift+Y 
    protected static final int REQUESTFAILED= 1; 
    protected static final int REQUESTEXCEPTION = 2;
    private EditText et_path;
    private TextView tv_result;

    //在主线程中定义一个Handler
    private Handler handler = new Handler(){
        //此方法在主线程中执行
        public void handleMessage(android.os.Message msg) {
            //所以可在主线程更新ui
            switch (msg.what) {
            case REQUESTSUCCESS:
                String content = (String) msg.obj;
                tv_result.setText(content);
                break;
            case REQUESTFAILED:
                Toast.makeText(getApplicationContext(), "请求资源不存在", Toast.LENGTH_LONG).show();
                break;
            case REQUESTEXCEPTION:
                Toast.makeText(getApplicationContext(), "服务器忙,请稍后再访问...", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
        };
    };
    
    @SuppressLint("NewApi") 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        tv_result = (TextView) findViewById(R.id.tv_result);
//        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
//        StrictMode.setThreadPolicy(policy);
        System.out.println("当前线程的名称:"+Thread.currentThread().getName());
    }


    // 点击按钮查看,获取指定路径的源码
    public void click(View v) {
        // 2.0创建一个子线程
        new Thread() {
            public void run() {
                try {
                    // 2.1获取源码路径
                    String path = et_path.getText().toString().trim();
                    // 2.2创建url对象,指定我们要访问的网址(路径)
                    URL url = new URL(path);
                    // 2.3拿到HttpURLConnection对象,用于发送或接收数据
                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    // 2.4设置发送get请求
                    urlConnection.setRequestMethod("GET");// get要求大写,默认为get请求
                    // 2.5设置请求的超时时间
                    urlConnection.setConnectTimeout(5000);
                    // 2.6获取服务器返回的状态码 200请求成功
                    int code = urlConnection.getResponseCode();
                    if (code == 200) {
                        // 2.7获取服务器返回的数据,以流的形式返回
                        InputStream inputStream = urlConnection
                                .getInputStream();
                        String content = StreamTools.readStream(inputStream);
                        // 2.8.0创建Message对象
                        Message msg = new Message();
                        msg.what = REQUESTSUCCESS;
                        msg.obj = content;
                        // 2.8.1拿着我们创建的Handler助手,告诉系统,我们需要更新ui
                        handler.sendMessage(msg); // 发了一条消息,把消息放到msg里,handlerMessage方法执行
                        // 2.8将流的数据展示到TextView
                        // tv_result.setText(content);
                    } else {
                        // 请求资源不存在 Toast是一个view 也不能在子线程更新ui
                        Message msg = new Message();
                        msg.what = REQUESTFAILED; // 代表哪条消息
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    Message msg = new Message();
                    msg.what = REQUESTEXCEPTION; // 代表哪条消息
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }
        }.start();

    }
    
}




public class StreamTools {

    //把一个InputStream 转换成String
    
    public static String readStream(InputStream in) throws Exception{
        //定义一个内存输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024]; //1kb
        while((len = in.read(buffer))!= -1){
            baos.write(buffer, 0, len);
        }
        in.close();
        String content = new String(baos.toByteArray());
        return content;
    }
}
View Code

相关文章:

  • 2021-09-04
  • 2021-08-21
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-01-14
  • 2021-12-24
相关资源
相似解决方案