Gson使用简记
来源:互联网 发布:企业邮箱需要域名吗 编辑:程序博客网 时间:2024/06/05 20:25
在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<String>() { @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<ResultsBean>
:
public class MessageBean { private boolean status; private String info; private List<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 List<ResultsBean> getResults() { return results; } public void setResults(List<ResultsBean> 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<String>() { @Override public void onResponse(String response) { Gson gson = new Gson(); MessageBean messageBean = gson.fromJson(response, MessageBean.class); List<MessageBean.ResultsBean> 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); }
0 0
- Gson使用简记
- Gson 简记Demo
- 【Gson】Gson 使用笔记
- sql 游标使用简记
- CMFCPropertyGridCtrl 使用简记
- CMFCPropertyGridCtrl 使用简记
- 编译宏使用简记
- TortoiseSVN使用简记
- maven简单使用简记
- Java Applet使用简记
- git 使用简记
- CMFCPropertyGridCtrl 使用简记
- signaltap使用简记
- git使用简记
- setProjectionMap@SQLiteQueryBuilder使用简记
- Gradle使用简记
- hibernate使用简记
- Java Applet使用简记
- django进阶5 自定义标签
- ZOJ 3710 Friends
- iOS编程--AVCapture编程理解--自定义相机
- C语言结构体中定义函数指针
- 编程技巧 - NSString - 1
- Gson使用简记
- git分支操作整理
- concurrenthashmap 采用自动调整大小的数组锁,是不是效率更高一点呢?
- UE4启动器是否有办法添加本地版本
- 编译配置nginx-rtmp-moule
- android端获取网络数据添加到二级列表ExpandableListView
- iOS runtime运行机制
- 在UIView中抠透明区域
- android中文教程