使用Xutils3 + Gson请求服务器数据并解析(大创培训)

1 效果展示

使用Xutils3 + Gson请求服务器数据并解析(大创培训)_第1张图片
列表显示友约
使用Xutils3 + Gson请求服务器数据并解析(大创培训)_第2张图片
友约详情

2 实现涉及技术

2.1 接口文档阅读

使用Xutils3 + Gson请求服务器数据并解析(大创培训)_第3张图片
image.png
使用Xutils3 + Gson请求服务器数据并解析(大创培训)_第4张图片
image.png

2.2 Xutils3请求数据、Gson解析数据

Xutils3请求数据
https://blog.csdn.net/qpc908694753/article/details/70463709

Gson解析数据
https://www.cnblogs.com/jianyungsun/p/6647203.html

百度云盘视频
https://pan.baidu.com/s/1s3MVvsfM588baeY1ZSGxHQ
密码:xsdv

2.3 PullToRefreshListView实现下拉刷新和上拉加载更多

PullToRefreshListView使用方法
https://blog.csdn.net/lisdye2/article/details/51449716

2.4 Intent开启Activity,并传递新参数

Intent使用方法
https://blog.csdn.net/u013921245/article/details/49005369

3 具体实现

3.1 接口文档 + Xutils3请求数据 + Gson解析数据

        String url = AppNetConfig.BASEURL + AppNetConfig.SEPARATOR + AppNetConfig.READ + AppNetConfig.SEPARATOR + AppNetConfig.ABOUTSAVE;//常量类管理网络请求常量
        RequestParams params = new RequestParams(url);//参数
        params.addParameter(AppConstants.ACCOUNT,UserManage.getInstance().getUserInfo(mActivity).account);//所需参数1
        params.addParameter(AppConstants.TYPE,"a");//所需参数2
        x.http().request(HttpMethod.GET, params, new Callback.CommonCallback() {

            @Override
            public void onSuccess(String result) {
                //请求成功   上面 result 代表服务端返回结果
                Gson gson = new Gson();//使用Gson解析数据
                final Collect_Date_Info collect_date_info = gson.fromJson(result, Collect_Date_Info.class);
                if(collect_date_info.code == 200){//根据状态码判断是否解析成功
                    pullMydateRefresh.setAdapter(new MyCollectAdapter(collect_date_info));//给pullMydateRefresh填充数据
                    //设置某个item被点击的监听
                    pullMydateRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView parent, View view, int position, long id) {
                            Intent intent = new Intent(mActivity, DateDetailActivity.class);
                            Collect_Date_Info.Collect_Date_Detail collect_date_details = collect_date_info.data.get(position - 1);//pullMydateRefresh的下标从1开始
                            //通过intent传值
                            intent.putExtra("activity_id", collect_date_details.activity_id);
                            intent.putExtra("save_id", collect_date_details.save_id);
                            mActivity.startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                CommonUtils.showToastShort(mActivity,"获取友约收藏数据失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

3.2 填充详情页面

 pullMydateRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    Intent intent = new Intent(mActivity, DateDetailActivity.class);
                    DateNews.DateNews_Detail dateNews_detail = mDatadetail.get(position - 1);
                    intent.putExtra("activity_id", dateNews_detail.activity_id);
                    intent.putExtra("picUrl", dateNews_detail.picture);
                    intent.putExtra("title", dateNews_detail.title);
                    intent.putExtra("content", dateNews_detail.content);
                    intent.putExtra("readNum", dateNews_detail.readNum + "");
                    intent.putExtra("agreeNum", dateNews_detail.agreeNum + "");
                    intent.putExtra("commentNum", dateNews_detail.commentNum + "");
                    intent.putExtra("endTime", dateNews_detail.endTime);
                    intent.putExtra("startTime", dateNews_detail.startTime);
                    intent.putExtra("memberNum", dateNews_detail.memberNum + "");
                    intent.putExtra("type", dateNews_detail.type + "");
                    intent.putExtra("place", dateNews_detail.place);
                    mActivity.startActivity(intent);
                }
            });
private void setDataView(DateNews_detalis.DateNews_content dateNewsContent) {
        String dataTime = "活动时间:" + dateNewsContent.startTime + " - " + dateNewsContent.endTime.substring(dateNewsContent.endTime.indexOf(" ") + 1, dateNewsContent.endTime.length());
        String joinTime = "报名时间:" + dateNewsContent.beginTime + " - " + dateNewsContent.deadline.substring(dateNewsContent.deadline.indexOf(" ") + 1, dateNewsContent.deadline.length());
        String joinPeople = "已报名:" + dateNewsContent.memberNum + "/" + dateNewsContent.memberTotalNum + "人";
        if (TextUtils.isEmpty(dateNewsContent.picture)) {
            Picasso.with(mActivity).load(R.drawable.nopic1).into(ivDatePicture);
        } else {
            String pic = AppNetConfig.BASEURL + AppNetConfig.SEPARATOR + AppNetConfig.PICTURE + AppNetConfig.SEPARATOR + dateNewsContent.picture;
            Picasso.with(mActivity).load(pic).into(ivDatePicture);
        }
        tvDateTitle.setText(dateNewsContent.title);
        ivSign.setImageResource(R.drawable.sign);
        tvDateDate.setText(dataTime);
        ivBrowse.setImageResource(R.drawable.read_num);
        tvReadNum.setText(String.valueOf(dateNewsContent.readNum));
        ivShare.setImageResource(R.drawable.share);
        tvShareNum.setText(String.valueOf(dateNewsContent.commentNum));
        tvDateContent.setText(dateNewsContent.content);
        ivClock.setImageResource(R.drawable.time);
        tvClock.setText(joinTime);
        ivLocate.setImageResource(R.drawable.locate);
        tvLocate.setText("地点:" + dateNewsContent.place);
        ivAttend.setImageResource(R.drawable.join_people);
        tvAttend.setText(joinPeople);
        ivContact.setImageResource(R.drawable.consult_small);
        tvContact.setText("发起人:" + dateNewsContent.proposer);

        ivConsult.setImageResource(R.drawable.consult);
        tvConsult.setText("咨询");
        ivMark.setImageResource(R.drawable.mark);
        if (!iscollect) {
            tvMark.setText("收藏");
            tvMark.setTextColor(0xffBCBCBC);
        } else {
            tvMark.setText("已收藏");
            tvMark.setTextColor(0xff87CEEB);
        }
        tvJoinIn.setBackgroundResource(R.color.main_orange);
        tvJoinIn.setText("报名参加");
    }

你可能感兴趣的:(使用Xutils3 + Gson请求服务器数据并解析(大创培训))