自定义GsonRequest
来源:互联网 发布:个人博客域名备案 编辑:程序博客网 时间:2024/06/05 18:25
原文地址 http://www.tuicool.com/articles/QbiU3q
自定义GsonRequest
JsonRequest的数据解析是利用Android本身自带的JSONObject和JSONArray来实现的,配合使用JSONObject和JSONArray就可以解析出任意格式的JSON数据。不过也许你会觉得使用JSONObject还是太麻烦了,还有很多方法可以让JSON数据解析变得更加简单,比如说GSON。遗憾的是,Volley中默认并不支持使用自家的GSON来解析数据,不过没有关系,通过上面的学习,相信你已经知道了自定义一个Request是多么的简单,那么下面我们就来举一反三一下,自定义一个GsonRequest。
首先我们需要把gson的jar包添加到项目当中,jar包的下载地址是:https://code.google.com/p/google-gson/downloads/list 。
接着定义一个GsonRequest继承自Request,代码如下所示:
public class GsonRequest<T> extends Request<T> { private final Listener<T> mListener; private Gson mGson; private Class<T> mClass; public GsonRequest(int method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { super(method, url, errorListener); mGson = new Gson(); mClass = clazz; mListener = listener; } public GsonRequest(String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { this(Method.GET, url, clazz, listener, errorListener); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(mGson.fromJson(jsonString, mClass), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(T response) { mListener.onResponse(response); }}可以看到,GsonRequest是继承自Request类的,并且同样提供了两个构造函数。在parseNetworkResponse()方法中,先是将服务器响应的数据解析出来,然后通过调用Gson的fromJson方法将数据组装成对象。在deliverResponse方法中仍然是将最终的数据进行回调。
那么下面我们就来测试一下这个GsonRequest能不能够正常工作吧,调用http://www.weather.com.cn/data/sk/101010100.html 这个接口可以得到一段JSON格式的天气数据,如下所示:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"19","WD":"南风","WS":"2级","SD":"43%","WSE":"2","time":"19:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}接下来我们使用对象的方式将这段JSON字符串表示出来。新建一个Weather类,代码如下所示:
public class Weather { private WeatherInfo weatherinfo; public WeatherInfo getWeatherinfo() { return weatherinfo; } public void setWeatherinfo(WeatherInfo weatherinfo) { this.weatherinfo = weatherinfo; }}Weather类中只是引用了WeatherInfo这个类。接着新建WeatherInfo类,代码如下所示:
public class WeatherInfo { private String city; private String temp; private String time; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getTime() { return time; } public void setTime(String time) { this.time = time; }}WeatherInfo类中含有city、temp、time这几个字段。下面就是如何调用GsonRequest了,其实也很简单,代码如下所示:
GsonRequest<Weather> gsonRequest = new GsonRequest<Weather>( "http://www.weather.com.cn/data/sk/101010100.html", Weather.class, new Response.Listener<Weather>() { @Override public void onResponse(Weather weather) { WeatherInfo weatherInfo = weather.getWeatherinfo(); Log.d("TAG", "city is " + weatherInfo.getCity()); Log.d("TAG", "temp is " + weatherInfo.getTemp()); Log.d("TAG", "time is " + weatherInfo.getTime()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } });mQueue.add(gsonRequest);可以看到,这里onResponse()方法的回调中直接返回了一个Weather对象,我们通过它就可以得到WeatherInfo对象,接着就能从中取出JSON中的相关数据了。现在运行一下代码,观察控制台日志,打印数据如下图所示:
这样的话,GsonRequest的功能就基本都实现了,我们也是借助这个例子深刻地理解了自定义Request的方法,对Volley的认识也是更加深入了。好了,本篇文章就到此结束,下篇文章中我们将对Volley进行更深层次的研究。
0 0
- 自定义GsonRequest
- Volley 自定义XMLRequest和GSONRequest
- 自定义Volley的GsonRequest使用
- Android-Volley网络通信框架(自定义Request 请求:实现 GsonRequest)
- Volley源码分析之自定义GsonRequest(带header,coockie,Json参数,Gson解析)
- Volley XMLRequest GsonRequest 笔记
- 基于Volley的GsonRequest
- Android Volley完全解析之定制自己的Request XMLRequest GsonRequest
- 基于Volley,Gson封装支持JWT无状态安全验证和数据防篡改的GsonRequest网络请求类
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义TexBox,自定义ComboBox
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- java中random方法以及package
- 网页实现从数据库读取数据并简单分页
- 一行代码获取文件的后缀名
- Redis和Memcached的区别
- @date2016-03-24(软件工程男的日常)
- 自定义GsonRequest
- @date2016-03-25(软件工程男的日常)
- OC中几种延时操作的比较
- HDOJ 2040 亲和数
- [Android开发]知乎日报
- error C2039: 'GetCurSel' : is not a member of 'CString'
- 乱入的'\0'
- Codeforces 622E (树DP)
- spring学习笔记(23)基于tx/aop配置切面增强事务