Android入门之 网络访问 :

ListView控件, 网络获取Json数据, 并将数据展示到listView中 的步骤:

  1. 先布局 (先对当前页面整体, 再为ListView中的每一个行进行单独的 行布局)
  2. 通过异步任务 从网络上 获取JSON串, 解析为对象,获得数据集合
  3. 编写ListView的适配器(借助适配器, 根据前面获得的数据集合来 为ListView框架 填充数据(text, image…))

1 布局无需多说
2.异步任务(AsyncTask类/相当于Thread类): 可以实现与UI(主线程)交互的线程框架。
为什么要使用异步任务 来执行网络获取操作?
Because:
在这里插入图片描述

使用:3个泛型,4个步骤 说明:
在这里插入图片描述

完整的 自定义异步任务:

    class MyAsyncTask extends AsyncTask<String, Void, String>{
        StringBuilder sb;
        // "事前" 执行
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }
        // "事中" 执行
        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");

                connection.setConnectTimeout(1000 * 5);

                if (connection.getResponseCode() == 200){
                    InputStream is = connection.getInputStream();

                    byte[] bys = new byte[1024];
                    int len = 0;

                    sb = new StringBuilder();
                    while((len = is.read(bys)) != -1){
                        sb.append(new String(bys, 0, len));
                    }
                }

                return sb.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        // "事后" 执行: 并接收 "事中"返回的 结果
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Gson gson = new Gson();
            Food_Material food_material = gson.fromJson(s, Food_Material.class);
            List<Food_Material.DishsBean> dishs = food_material.getDishs();
            for (Food_Material.DishsBean dish:dishs) {
                String img = dish.getImg(); //图片
//                new MyImg().execute(img);

                String name = dish.getName(); //1
                String burdens = dish.getBurdens(); //2
                String allClick = dish.getAllClick(); //4
                String favorites = dish.getFavorites(); //5
                Food_Material.DishsBean.CustomersBean customers = dish.getCustomers();
                String nickName = customers.getNickName(); //3

                beans.add(new Bean(img, name, burdens, nickName, allClick, favorites));
            }

            /**
             *  适配器 填充数据
             */
            MyBaseAdapter adapter = new MyBaseAdapter(beans, First_Activity.this);
            Lv.setAdapter(adapter);

        }
        // 进度更新(不会自动调用, 需要手动调用) 一般与"事中"搭配使用: 用于UI界面的实时变化
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);

        }
    }

3 适配器(Adapter):
@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null){

    convertView = LayoutInflater.from(ct).inflate(R.layout.list_row, null);

    ImageView img = convertView.findViewById(R.id.img);
    TextView tv_01 = convertView.findViewById(R.id.name);
    TextView tv_02 = convertView.findViewById(R.id.burdens);
    TextView tv_03 = convertView.findViewById(R.id.nickName);
    TextView tv_04 = convertView.findViewById(R.id.allClick);
    TextView tv_05 = convertView.findViewById(R.id.favorites);
 注意:   // 该 语句 直接通过网址 下载图片, 然后 放到当前Position位置的ImageView/ImageButton 控件 !!
    Picasso.get().load(beans.get(position).getImg()).into(img);
    tv_01.setText(beans.get(position).getName());
    tv_02.setText(beans.get(position).getBurdens());
    tv_03.setText(beans.get(position).getNickName());
    tv_04.setText(beans.get(position).getAllClick()+"浏览");
    tv_05.setText(beans.get(position).getFavorites()+"收藏");


}

return convertView;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值