解析中括号json文件

public class Fragmentxinsanban extends Fragment {

    private PullToRefreshListView refreshListView;
    private List list = new ArrayList<>();
    private MyAdapter2 myAdapter;
    private int page_num = 1;
    private ILoadingLayout startLabels;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.xinsanban_view,container,false);

        refreshListView = view.findViewById(R.id.xinsanban_list_view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //1.设置listView的适配器
        getDataFromNet();


        //2.设置刷新模式
        /*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            //下拉刷新的时候调用的方法....请求第一页的数据,清空之前的数据,然后再添加设置适配器
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                String path = "http://mnews.gw.com.cn/wap/data/news/news/mobile/mgxw/page_1.json";

                NetDataUtil.getData(path, getActivity(), new JsonCallBack() {
                    @Override
                    public void getJsonString(String json) {
                        JSONArray jsonArray=null;
                        try {
                            jsonArray = new JSONArray(json);
                            String s = jsonArray.get(0).toString();
                            Log.d("TAG","444+++++++++++++++++++++++++++++"+s);
                            //解析
                            Gson gson = new Gson();

                            LunBoBean lunBoBean = gson.fromJson(s, LunBoBean.class);
                            //先清空一下数据
                            list.clear();

                            //添加到集合的最前边,,,,(0,,,,)
                            list.addAll(0,lunBoBean.getData());

                            //设置适配器
                            setAdapter();

                            //设置适配器之后停止刷新的操作
                            refreshListView.onRefreshComplete();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                        //可以设置刷新的时间....
//                        startLabels.setLastUpdatedLabel("上次更新时间:"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis())));//last最近的,最后一次update修改/更新
                    }
                });
            }

            //上拉刷新的时候调用的方法..page++,然后在请求数据
            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                page_num++;
                getDataFromNet();
            }
        });

    }


    /**
     * 获取网络数据
     */
    private void getDataFromNet() {
        String path = "http://mnews.gw.com.cn/wap/data/news/news/mobile/mgxw/page_";

        //第一个参数是接口,第二个上下文,第三个回调json数据
        NetDataUtil.getData(path+page_num+".json", getActivity(), new JsonCallBack() {
            @Override
            public void getJsonString(String json) {
                JSONArray jsonArray=null;
                try {
                    jsonArray = new JSONArray(json);
                    String s = jsonArray.get(0).toString();
                    Log.d("TAG","333+++++++++++++++++++++++++++++"+s);
                    //解析
                    Gson gson = new Gson();

                    LunBoBean lunBoBean = gson.fromJson(s, LunBoBean.class);

                    //往后面添加...
                    list.addAll(lunBoBean.getData());

                    //设置适配器
                    setAdapter();

                    //停止刷新
                    refreshListView.onRefreshComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void setAdapter() {
        if (myAdapter == null){

            myAdapter = new MyAdapter2(getActivity(),list);
            refreshListView.setAdapter(myAdapter);

        }else {
            myAdapter.notifyDataSetChanged();
        }
    }
}

你可能感兴趣的:(解析中括号json文件)