【Android开发新手的学习笔记】异步加载知识梳理

用异步加载的方式实现一个图文混排的ListView。



1.在安卓中,耗时任务要单独放在一个线程中做,不能阻塞主线程。用异步加载的方式去读取数据,在使用的时候才不会感觉到卡顿。

2.用多线程或AsyncTask去实现异步加载,两种方式异曲同工,AsyncTask也是用Tread实现的。

使用的获取数据的接口是慕课网的接口:

http://www.imooc.com/api/teacher?type=4&num=30

能够获取到如上图所示的一些JSON格式的数据,因为发送网络请求是属于耗时任务,所以需要使用异步加载的方式去实现


整体思路:

1.重写AsyncTask的doInBackground()方法,这个方法用于在后台获取接口里面的JSON格式数据,然后将JSON格式的数据,转化为一个List返回。

public List<NewsBean> getJsonData(String url) {

    List<NewsBean> newsBeanList =new ArrayList<>();
    try {
        String jsonData=readStream(new URL(url).openStream());
        JSONObject jsonObject=new JSONObject(jsonData);
        JSONArray jsonArray=jsonObject.getJSONArray("data");
        for(int i=0; i<jsonData.length();i++) {
            NewsBean newsBean=new NewsBean();
            newsBean.newsIconUrl=jsonArray.getJSONObject(i).getString("picSmall");
            newsBean.newsContent=jsonArray.getJSONObject(i).getString("description");
            newsBean.newsTitle=jsonArray.getJSONObject(i).getString("name");
            newsBeanList.add(newsBean);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return newsBeanList;
}

public String readStream(InputStream is){
    InputStreamReader isr;
    String result="";
    try {
        String line ="";
        isr=new InputStreamReader(is,"utf-8");
        BufferedReader br=new BufferedReader(isr);
        while ((line=br.readLine())!=null){
            result+=line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
2.重写AsyncTask的onPostExecute()方法

在doInBackground()方法中获取到的JSON格式数据,被存到一个List里,传到这个方法里。再在这个方法内更新UI,为这个List设置一个Adapter。

要更新图片,要先将List中的url连接转换为BitMap,所以要新建一个ImageLoader类,在这里,也是可以用多线程的方式或AsyncTask的方式实现。

public class ImageLoader {

    ImageView imageView;
    String url;

    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(url.equals(imageView.getTag())) {
                imageView.setImageBitmap((Bitmap) msg.obj);
            }
        }
    };

    public void showImageByTread( ImageView ivIcon, final String newsIconUrl) {
        imageView =ivIcon;
        url=newsIconUrl;
        new Thread() {
            @Override
            public void run() {
                super.run();
                Message message=Message.obtain();
                message.obj=getBitmapFromUrl(newsIconUrl);
                handler.sendMessage(message);

            }
        }.start();
    }

    private Bitmap getBitmapFromUrl(String newsIconUrl) {
        Bitmap bitmap;
        InputStream is = null;

        try {
            URL url =new URL(newsIconUrl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            is=new BufferedInputStream(httpURLConnection.getInputStream());
            bitmap= BitmapFactory.decodeStream(is);
            httpURLConnection.disconnect();
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值