Gson使用简记

在APP的开发过程中,必不可少的就是网络请求,请求服务器,得到数据,解析数据并加载到控件上,实现了APP的动态数据显示。
在解析数据的方法上也是多种多样的,之前用的都是JsonObject、JsonArray直接解析出来,后来发现用Gson配合Android Studio的 GsonFormat可以更加快速地解析数据。

首先,导入Gson包,在Android Studio的gradle里面添加:

dependencies {
    compile 'com.google.code.gson:gson:2.6.2'
}

服务器返回值如下:

{
    "status": true, 
    "info": "获取成功", 
    "results": {
        "version": "1", 
        "title": "测试标题", 
        "url": "http://www.baidu.com"
    }}

然后,用GsonFormat生成Bean文件:

public class WelcomePic {


    /**
     * status : true
     * info : 获取成功
     * results : {"version":"1","title":"测试标题","url":"http://www.baidu.com"}
     */

    private boolean status;
    private String info;
    /**
     * version : 1
     * title : 测试标题
     * url : http://www.baidu.com
     */

    private ResultsBean results;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public ResultsBean getResults() {
        return results;
    }

    public void setResults(ResultsBean results) {
        this.results = results;
    }

    public static class ResultsBean {
        private int version;
        private String title;
        private String url;

        public int getVersion() {
            return version;
        }

        public void setVersion(int version) {
            this.version = version;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

获取网路数据用的是Volley:

 /**
     * 获取欢迎界面的图片
     */
    private void getWelcome() {

        StringRequest stringRequest = new StringRequest(Baseurl.Welcome, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                WelcomePic welcomePic = gson.fromJson(response,WelcomePic.class);
                WelcomePic.ResultsBean resultsBean = welcomePic.getResults();
                if (welcomePic.isStatus()){
                      ToastUtil.showToast(context,
resultsBean.getTitle()+resultsBean.getUrl()+resultsBean.getVersion());
                    } 
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        queue.add(stringRequest);
    }

如果返回的数据包含了列表,原理类似:

{
    "status": true, 
    "info": "获取信息成功", 
    "results": [
        {
            "id": "199", 
            "title": "APP1.1版本更新", 
            "classid": "1", 
            "info": "APP1.1版本更新,请更新APP", 
            "time": "2016-01-18 15:26:54", 
            "see": "1", 
            "type": "系统"
        }, 
        {
            "id": "198", 
            "title": "欢迎使用APP, 
            "classid": "2", 
            "info": "欢迎使用APP,希望你喜欢", 
            "time": "2016-01-18 15:27:55", 
            "see": "1", 
            "type": "通知"
        }
    ]
}

GsonFormat生成的Bean与之前的有点区别,前面的ResultsBean,后面的是List

public class MessageBean {

    private boolean status;
    private String info;
    private List results;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public List getResults() {
        return results;
    }

    public void setResults(List results) {
        this.results = results;
    }

    public static class ResultsBean {
        private String id;
        private String title;
        private String classid;
        private String info;
        private String time;
        private int see;
        private String type;
        private String url;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getClassid() {
            return classid;
        }

        public void setClassid(String classid) {
            this.classid = classid;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public int getSee() {
            return see;
        }

        public void setSee(int see) {
            this.see = see;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

处理返回的数据:

 private void GetInfolist() {

        String path = ""
        StringRequest string = new StringRequest(path, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                MessageBean messageBean = gson.fromJson(response, MessageBean.class);
                List resultsBeen = messageBean.getResults();
                if (messageBean.isStatus()) {
                    for (int i = 0; i < resultsBeen.size(); i++) {
                        if (resultsBeen.get(i).getSee() == 0) {
                            resultsBeen.get(i).setSee(R.mipmap.ico_message_issee);
                        } else {
                            resultsBeen.get(i).setSee(Color.parseColor("#080809"));
                        }
                        messageBean.setResults(resultsBeen);
                    }
                    list.add(messageBean);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        mqueue.add(string);
    }

你可能感兴趣的:(【Android工具类】)