网络衔接拿数据进行Gson解析并放入ListView进行展示

不要
*

  • @Date:2019/2/15 14:10

  • @Description:描述信息
    */
    public class frag01 extends Fragment {

    private ListView listView;
    String strURL = “https://www.apiopen.top/novelApi”;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

     View view = inflater.inflate(R.layout.frag01, container, false);
     listView = view.findViewById(R.id.listview);
     return view;
    

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new Thread() {
    @Override
    public void run() {
    super.run();

             boolean isnNet = HttpUitl.isNetworkConnection(getActivity());
             String s = "";
             if (isnNet) {
                 s = HttpUitl.requesString(strURL);
             } else {
    
             }
             Message message = new Message();
             message.what = 0;
             message.obj = s;
             handler.sendMessage(message);
    
         }
     }.start();
    

    }

    Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    switch (msg.what) {
    case 0:
    String s= (String) msg.obj;

                 Gson gson=new Gson();
                  JsonBean jsonBean= gson.fromJson(s, JsonBean.class);
                  ArrayList<JsonBean.DataBean> list= (ArrayList<JsonBean.DataBean>) jsonBean.getData();
                 listView.setAdapter(new Adapter(getActivity(),list));
    
                 break;
    
         }
     }
    

    };
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值