AsyncTask异步任务类访问网络

AsyncTask异步访问网络,ui线程做耗时操作,会造成线程阻塞。因此只能把获取网络放在子线程操作     


       写个方法继承AsyncTask  

     使用异步任务的规则:1.声明一个类继承AsyncTask,标注3个参数类型。

                                              2.第一个参数表示执行的获取网络的路径

                                                  第二个参数表示进度的刻度

                                                   第三个参数表示返回的结果

                                              


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button = (android.widget.Button) findViewById(R.id.btn);
    ImageView = (android.widget.ImageView) findViewById(R.id.image);
    dialog = new ProgressDialog(this);
    dialog.setTitle("提示信息");
    dialog.setMessage("正在下载,请稍后....");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   dialog.setCancelable(false);
    Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Myantask().execute();//调用获取网络的方法
        }
    });

}


         

/**
 * 执行下载前的任务类
 */
public class Myantask extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }
/**
 * 网络获取的操作
 */
@Override
protected Bitmap doInBackground(String... params) {

    Bitmap bitmap = null;
    InputStream is = null;
    try {
        URL url = new URL("http://img1.gtimg.com/news/pics/hv1/37/195/1468/95506462.jpg");
        HttpURLConnection Connection = (HttpURLConnection) url.openConnection();
        Connection.setDoOutput(true);
        Connection.setDoInput(true);
        Connection.setConnectTimeout(10000);
        Connection.setRequestMethod("GET");
        Connection.connect();
        if (Connection.getResponseCode() == 200) {
            is = Connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}
 /*
       * 跟新UI的操作
       * */
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            ImageView.setImageBitmap(bitmap);

        }
 布局代码:
    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asus_pc.hander.MainActivity">


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:src="@mipmap/ic_launcher"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="下载网络图片" />
</RelativeLayout>
    

   
   
  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值