Android基础知识点梳理(4)

  • httpUrlconnection示例查看网页源码
  • 利用HttpURLConnection访问网站获取源码

public void click(View v){

            new Thread(){

                    public void run() {

                            try {

                                String path=et_path.getText().toString().trim();

                                URL url=new URL(path);

                                    HttpURLConnection conection=(HttpURLConnection) url.openConnection();

                                    conection.setRequestMethod("GET");//默认就是gei请求

                                    conection.setConnectTimeout(5000);

                                    int code=conection.getResponseCode();

                                    if(code==200){

                                            InputStream in=conection.getInputStream();

                                            String content=StreamTools.readStream(in);

 

                                            //使用助手告诉系统我要更新UI

                                            Message msg=new Message();

                                            msg.obj=content;

                                            handler.sendMessage(msg);

                                            //随后Handelmessage方法就会执行

                                    }

                                    

                        } catch (Exception e) {

                                    e.printStackTrace();

                            }

                    };

            }.start();

            

    }

 

private Handler handler=new Handler(){

@Override

public void handleMessage(Message msg) {

 

String m=(String) msg.obj;

tv_result.setText(m);

}

};

 

public class StreamTools {

public static String readStream(InputStream in) throws IOException{

int len=-1;

//内存输出流

ByteArrayOutputStream baos=new ByteArrayOutputStream();

byte[] buffer=new byte[10240];

while((len=in.read(buffer))!=-1){

baos.write(buffer,0,len);

}

in.close();

String content=new String(baos.toByteArray());

return content;

}

}

  • 为何需要消息机制
  • 主线程中进行了耗时操作或者阻塞(比如连接网络,或者拷贝大数据),就会报ANR application not response异常,需要避免这种情况发可以把耗时的操作放到子线程中来解决只有主线程(UI线程)才能更新UI
  • handler的基本用法

1.主线程定义一个Handler

2.使用Handler重写HandleMessage()方法

3.在子线程中使用穿送相应的数据handler.sendMessage(msg) msg来携带信息

  • 示例获取网页图片

 

if(code==200){

            InputStream in=conn.getInputStream();

            Bitmap bm=BitmapFactory.decodeStream(in);

    Message msg=Message.obtain();//以后就用这个静态方法,池子好

    msg.obj=bm;

    handler.sendMessage(msg);

     }

 

  • runOnUIThread基本用法

runOnUIThread(action)在UI线程,则立即执行

若不是在UI线程,就发到消息队列中

不管你写在什么位置上调用,action都运行在UI线程中

action是一个Runable

run方法中写修改UI的语句

携带数据时用handler

注意

两种方式都需要在活动销毁时主动调用cancel()销毁

  • 常见的消息api

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

System.out.println("五秒之后我就出来");

}

},5000);

这个run方法可以执行UI更新(已经封装好了)

new Timer().schedule(new TimerTask() {

 

@Override

public void run() {

System.out.println("三秒后出来!每过一秒一次");

 

}

}, 3000, 1000);

这个run方法不行不能执行UI(就是一个普通线程)

  • 开源项目smartimageview

setImageUrl();内部封装了一个获取数据,封装成bitmap在展示图片的功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值