当前位置: 移动技术网 > IT编程>移动开发>Android > Android 网络html源码查看器详解及实例

Android 网络html源码查看器详解及实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

邓朴方携巨款出逃,klwtblfs.exe,网游之星际帝国

android 网络html源码查看器详解及实例

io字节流的数据传输了解

handler的基本使用

1.作品展示

2.需要掌握的知识

fileinputstream,fileoutputstream,bufferinputstream,bufferoutstream的读写使用与区别

//进行流的读写
     byte[] buffer = new byte[1024 * 8];
     //创建一个写到内存的字节数组输出流
     bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();
     int len;
     while ((len = inputstream.read(buffer)) != -1) {
       bytearrayoutputstream.write(buffer,0,len);
       bytearrayoutputstream.flush();//不断的刷新缓存
     }
     bytearrayoutputstream.close();//进行关闭流通道
     result = bytearrayoutputstream.tostring();//将写入的字节流转化为字符串

请求url地址的基本步骤

使用urlconnection请求一个url地址获取内容:

     //1.创建一个url对象
       url url = new url(url_str);
     //2.获取一个urlconnection对象
       httpurlconnection connection = (httpurlconnection)url.openconnection();
     //3.为urlconnection对象设置一些请求的参数,请求方式,连接的超时时间 
       connection.setrequestmethod("get");//设置请求方式
       connection.setconnecttimeout(1000*10);//设置超时时间
     //4.在获取url请求的数据前需要判断响应码,200 :成功,206:访问部分数据成功  300:跳转或重定向 400:错误 500:服务器异常
       int code = connection.getresponsecode();
       if(code == 200){
     //5.获取有效数据,并将获取的流数据解析成string
         inputstream inputstream = connection.getinputstream();
         string result = streamutils.streamtostring(inputstream);

handler消息机制的写法与基本原理

  1.主线程中创建一个handler
    private handler handler = new handler(){
        public void handlemessage(android.os.message msg) {


        };
    };
    2.重写handler的handlermessage方法

    3.子线程中创建一个message对象,将获取的数据绑定给msg
        message msg = new message();
        //另一种方式:message msg = messge.obtain;
        msg.obj = result;
    4.主线程中的handler对象在子线程中将message发送给主线程
        handler.sendmessage(msg);

    5.主线程中handlermessage方法接受子线程发来的数据,就可以做更新ui的操作。

3.知识详解

消息机制原理

  1.message:用来携带子线程中的数据。

  2.messagequeue:用来存放所有子线程发来的message.

  3.handler:用来在子线程中发送message,在主线程中接受message,处理结果

  4.looper:是一个消息循环器,一直循环遍历messagequeue,从messagequeue中取一个message,派发给handler处理。

 

                                                             handler原理.png

4.项目代码

public class mainactivity extends appcompatactivity {
  private edittext et_url;
  private button btn_looksource;
  private textview tv_sourceshow;


  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    //1,找到控件id
    et_url = (edittext)findviewbyid(r.id.ev_url);
    btn_looksource = (button)findviewbyid(r.id.btn_looksource);
    tv_sourceshow = (textview)findviewbyid(r.id.source_show);
    //2,设置点击事件
    btn_looksource.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        try {
          //3,获取url地址
          final string url_str = et_url.gettext().tostring().trim();
          //string results ="";
          //3.1进行判断url是否为空
          if (textutils.isempty(url_str)) {
            toast.maketext(mainactivity.this, "url地址不能为空的", toast.length_short).show();
            return;
          }
          system.out.println("oclick方法线程:"+thread.currentthread().getname());
          //创建子线程对象
          new thread(new runnable() {
            @override
            public void run() {

            try {
              system.out.println("oclick方法runnable线程:"+thread.currentthread().getname());
              //4,请求url地址
              //4.1创建一个url对象
              url url = new url(url_str);
              //4.2获取urlconnection对象
              httpurlconnection connection = (httpurlconnection) url.openconnection();
              //4.3位urlconnection设置请求参数
              connection.setrequestmethod("get");
              connection.setconnecttimeout(5 * 1000);
              //4.4获取请求响应码并进行判断
              int code = connection.getresponsecode();
              if (code == 200) {
                //4.5获取数据并且解析成字符串
                inputstream inputstream = connection.getinputstream();
                //4.6进行获取string数据读出来的
                string results = streamutils.streantosting(inputstream);
                //5,将数据显示到textview 上
                //tv_sourceshow.settext(results);
                //☆☆☆3.子线中创建一个message对象,为了携带子线程中获取的数据给主线程。
                //message msg = new message();
                //msg.obj = result;//将获取的数据封装到msg中。
                //☆☆☆4.使用handler对象将message发送到主线程。
                //handler.sendmessage(msg);
                message msg = new message();
                msg.obj = results;
                handler.sendmessage(msg);
              }
            }catch (exception e) {
              e.printstacktrace();
            }

          }
          }).start();


        }catch (exception e) {
          e.printstacktrace();
        }
      }
    });
  }
  //☆☆☆1.在主线程中创建一个handler对象
  private handler handler = new handler(){
    //☆☆☆2.重写handler的handlermessage方法,用来接收子线程中发来的消息
    public void handlemessage(android.os.message msg) {
      //☆☆☆5.接收子线程发送的数据,处理数据。
      string result = (string) msg.obj;
      //☆☆☆6.当前方法属于主线程可以做ui的更新
      //五.获取服务器返回的内容,显示到textview上
      tv_sourceshow.settext(result);
    };
  };
}

public class streamutils {
  public static string streantosting(inputstream inputstream){
    string result = "";//保存字符串
    try {
      //进行流的读写
      byte[] buffer = new byte[1024 * 8];
      //创建一个写到内存的字节数组输出流
      bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();
      int len;
      while ((len = inputstream.read(buffer)) != -1) {
        bytearrayoutputstream.write(buffer,0,len);
        bytearrayoutputstream.flush();//不断的刷新缓存
      }
      bytearrayoutputstream.close();//进行关闭流通道
      result = bytearrayoutputstream.tostring();//将写入的字节流转化为字符串
    }catch (exception e){
      e.printstacktrace();
    }
    return result;
  }
}

5.反思总结

1.对应web的http协议基本原理没有了解,所以不太能理解网络的传输

2.注意在配置文件中注册获取网络权限,差点就gg了

3.开始接触后台的一些逻辑代码有些不能很理解,不过这个过程中还是有收获的,慢慢进行吧

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网