使用fastjson和xUtils框架来获取聚合笑话数据

首先导入fastjson和xUtils包

使用XListView 实现上拉加载更多和下拉刷新

申请聚合数据笑话key

注意使用fastjson对应的json内容必需和聚合里的一样

bean代码如下

package com.app.juhejoke.bean;


import java.util.List;

/**
 * Created by Administrator on 2016/6/13.
 */


//    public String content;
//    public String hashId;
//    public String unixtime;
//    public String updatetime;
public class Joke {
    private int errorCode;
    private String reason;
    private Result result;

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
    public static class Result{
        private List data;

        public List getData() {
            return data;
        }

        public void setData(List data) {
            this.data = data;
        }

        public static class JokeData{
            public String content;
            public String hashId;
            public String unixtime;
            public String updatetime;

            public String getContent() {
                return content;
            }

            public void setContent(String content) {
                this.content = content;
            }

            public String getHashId() {
                return hashId;
            }

            public void setHashId(String hashId) {
                this.hashId = hashId;
            }

            public String getUnixtime() {
                return unixtime;
            }

            public void setUnixtime(String unixtime) {
                this.unixtime = unixtime;
            }

            public String getUpdatetime() {
                return updatetime;
            }

            public void setUpdatetime(String updatetime) {
                this.updatetime = updatetime;
            }

            @Override
            public String toString() {
                return "JokeData{" +
                        "content='" + content + '\'' +
                        ", hashId='" + hashId + '\'' +
                        ", unixtime='" + unixtime + '\'' +
                        ", updatetime='" + updatetime + '\'' +
                        '}';
            }
        }
    }



}

使用fastjson代码解析笑话 封装在一个Utils类里

 public static List getJokeData(String jsonHtml){

        Joke jokebean=new Joke();
        try {
           jokebean=JSON.parseObject(jsonHtml,Joke.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return jokebean.getResult().getData();
    }

在mainactivity里获取数据这里使用了XListView 实现上拉和下拉加载

 private void getData() {
        jaokeurl=url+"&page="+page+"&pagesize=10";
        new HttpUtils().send(HttpRequest.HttpMethod.GET,
                jaokeurl,
                new RequestCallBack() {
                    @Override
                    public void onSuccess(ResponseInfo responseInfo) {
                        String jsonHtml = responseInfo.result;
                        List temp = Utils.getJokeData(jsonHtml);

                        list.addAll(temp);
                        jokeAdapter.setList(list);
                        jokeAdapter.notifyDataSetChanged();
                        onLoadCompleted();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Utils.show("网络读取失败,请检查网络");
                    }
                });
    }

下载地址:http://download.csdn.net/detail/cxb0y/9570485

你可能感兴趣的:(android)