Volley 结合GSON或FastJson用法

前端之家收集整理的这篇文章主要介绍了Volley 结合GSON或FastJson用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_301_4@

自定义GSON类

@H_301_4@
 1@H_301_4@ public@H_301_4@ class@H_301_4@ GsonRequest<T> extends@H_301_4@ Request<T> {
@H_301_4@ 2@H_301_4@     private@H_301_4@ final@H_301_4@ Gson mGson = new@H_301_4@ Gson();
@H_301_4@ 3@H_301_4@     final@H_301_4@ Class<T> mClazz;
@H_301_4@ 4@H_301_4@     final@H_301_4@ Listener<T> mListener;
@H_301_4@ 5@H_301_4@     final@H_301_4@ Map<String,String> mHeaders;
@H_301_4@ 6@H_301_4@ 
 7@H_301_4@     public@H_301_4@ GsonRequest(String url,Class<T> clazz,Listener<T> listener,ErrorListener errorListener) {
@H_301_4@ 8@H_301_4@         this@H_301_4@(Method.GET,url,clazz,null@H_301_4@,listener,errorListener);
@H_301_4@ 9@H_301_4@     }
@H_301_4@10@H_301_4@ 
11@H_301_4@     public@H_301_4@ GsonRequest(int@H_301_4@ method,String url,Map<String,String> headers,@H_301_4@12@H_301_4@             Listener<T> listener,128); line-height:1.5!important">13@H_301_4@         super@H_301_4@(method,128); line-height:1.5!important">14@H_301_4@         this@H_301_4@.mClazz = clazz;
@H_301_4@15@H_301_4@         this@H_301_4@.mHeaders = headers;
@H_301_4@16@H_301_4@         this@H_301_4@.mListener = listener;
@H_301_4@17@H_301_4@     }
@H_301_4@18@H_301_4@ 
19@H_301_4@     @Override
@H_301_4@20@H_301_4@     public@H_301_4@ Map<String,String> getHeaders() throws@H_301_4@ AuthFailureError {
@H_301_4@21@H_301_4@         return@H_301_4@ mHeaders != null@H_301_4@ ? mHeaders : super@H_301_4@.getHeaders();
@H_301_4@22@H_301_4@     }
@H_301_4@23@H_301_4@ 
24@H_301_4@     @Override
@H_301_4@25@H_301_4@     protected@H_301_4@ void@H_301_4@ deliverResponse(T response) {
@H_301_4@26@H_301_4@         mListener.onResponse(response);
@H_301_4@27@H_301_4@     }
@H_301_4@28@H_301_4@ 
29@H_301_4@     @Override
@H_301_4@30@H_301_4@     protected@H_301_4@ Response<T> parseNetworkResponse(NetworkResponse response) {
@H_301_4@31@H_301_4@         try@H_301_4@ {
@H_301_4@32@H_301_4@             String json = new@H_301_4@ String(response.data,HttpHeaderParser.parseCharset(response.headers));
@H_301_4@33@H_301_4@             return@H_301_4@ Response.success(mGson.fromJson(json,mClazz),128); line-height:1.5!important">34@H_301_4@                     HttpHeaderParser.parseCacheHeaders(response));
@H_301_4@35@H_301_4@         } catch@H_301_4@ (UnsupportedEncodingException e) {
@H_301_4@36@H_301_4@             return@H_301_4@ Response.error(new@H_301_4@ ParseError(e));
@H_301_4@37@H_301_4@         } catch@H_301_4@ (JsonSyntaxException e) {
@H_301_4@38@H_301_4@             39@H_301_4@         }
@H_301_4@40@H_301_4@     }
@H_301_4@41@H_301_4@ }
@H_301_4@

自定义FastJson类

class@H_301_4@ FastJsonRequest<T> extends@H_301_4@ Request<T> { @H_301_4@ 5@H_301_4@ 6@H_301_4@ public@H_301_4@ FastJsonRequest(String url,128); line-height:1.5!important"> 7@H_301_4@ 8@H_301_4@ } @H_301_4@ 9@H_301_4@ 10@H_301_4@ public@H_301_4@ FastJsonRequest(11@H_301_4@ Listener<T> listener,128); line-height:1.5!important">12@H_301_4@ 16@H_301_4@ } @H_301_4@17@H_301_4@ 18@H_301_4@ @Override @H_301_4@19@H_301_4@ 20@H_301_4@ 21@H_301_4@ } @H_301_4@22@H_301_4@ 23@H_301_4@ @Override @H_301_4@24@H_301_4@ 25@H_301_4@ mListener.onResponse(response); @H_301_4@26@H_301_4@ } @H_301_4@27@H_301_4@ 28@H_301_4@ @Override @H_301_4@29@H_301_4@ 30@H_301_4@ 31@H_301_4@ String json = 32@H_301_4@ return@H_301_4@ Response.success(JSON.parSEObject(json,128); line-height:1.5!important">33@H_301_4@ HttpHeaderParser.parseCacheHeaders(response)); @H_301_4@34@H_301_4@ } 35@H_301_4@ 36@H_301_4@ } @H_301_4@37@H_301_4@ } @H_301_4@38@H_301_4@ }
@H_301_4@
class@H_301_4@ MainActivity extends@H_301_4@ Activity { @H_301_4@private@H_301_4@ Context context; @H_301_4@private@H_301_4@ String url="http://www.weather.com.cn/data/sk/101010100.html"; @H_301_4@private@H_301_4@ TextView text; @H_301_4@ 5@H_301_4@ 6@H_301_4@ @Override @H_301_4@void@H_301_4@ onCreate(Bundle savedInstanceState) { @H_301_4@super@H_301_4@.onCreate(savedInstanceState); @H_301_4@ 9@H_301_4@ setContentView(R.layout.activity_main); @H_301_4@10@H_301_4@ context=this@H_301_4@; @H_301_4@11@H_301_4@ text=(TextView)this@H_301_4@.findViewById(R.id.txttitle); @H_301_4@12@H_301_4@ RequestQueue mQueue = Volley.newRequestQueue(context); @H_301_4@13@H_301_4@ FastJsonRequest<Weather> fastJson=new@H_301_4@ FastJsonRequest<Weather>(url,Weather.class@H_301_4@,128); line-height:1.5!important">14@H_301_4@ new@H_301_4@ Response.Listener<Weather>() { @H_301_4@15@H_301_4@ 16@H_301_4@ @Override @H_301_4@17@H_301_4@ void@H_301_4@ onResponse(Weather weather) { @H_301_4@18@H_301_4@ //@H_301_4@ TODO Auto-generated method stub@H_301_4@ 19@H_301_4@ WeatherInfo weatherInfo = weather.getWeatherinfo(); @H_301_4@20@H_301_4@ text.setText(weatherInfo.getCity()); @H_301_4@21@H_301_4@ } @H_301_4@22@H_301_4@ },255); line-height:1.5!important">new@H_301_4@ Response.ErrorListener() { @H_301_4@24@H_301_4@ @Override @H_301_4@25@H_301_4@ void@H_301_4@ onErrorResponse(VolleyError arg0) { @H_301_4@26@H_301_4@ 27@H_301_4@ 28@H_301_4@ } @H_301_4@29@H_301_4@ }); @H_301_4@30@H_301_4@ mQueue.add(fastJson); @H_301_4@31@H_301_4@ 32@H_301_4@ GsonRequest<Weather> gsonRequest=new@H_301_4@ GsonRequest<Weather>(url,128); line-height:1.5!important">33@H_301_4@ 34@H_301_4@ 35@H_301_4@ @Override @H_301_4@36@H_301_4@ 37@H_301_4@ 38@H_301_4@ WeatherInfo weatherInfo = weather.getWeatherinfo(); @H_301_4@39@H_301_4@ text.setText(text.getText()+weatherInfo.getTime()); @H_301_4@40@H_301_4@ } @H_301_4@41@H_301_4@ },128); line-height:1.5!important">42@H_301_4@ 43@H_301_4@ @Override @H_301_4@44@H_301_4@ 45@H_301_4@ 46@H_301_4@ 47@H_301_4@ } @H_301_4@48@H_301_4@ }); @H_301_4@49@H_301_4@ mQueue.add(gsonRequest); @H_301_4@50@H_301_4@ 51@H_301_4@ 52@H_301_4@ } @H_301_4@53@H_301_4@ }
Model类

class@H_301_4@ Weather { @H_301_4@private@H_301_4@ WeatherInfo weatherinfo; @H_301_4@ 3@H_301_4@ public@H_301_4@ WeatherInfo getWeatherinfo() { @H_301_4@ 5@H_301_4@ return@H_301_4@ weatherinfo; @H_301_4@ 6@H_301_4@ } @H_301_4@ 7@H_301_4@ 8@H_301_4@ void@H_301_4@ setWeatherinfo(WeatherInfo weatherinfo) { @H_301_4@ 9@H_301_4@ this@H_301_4@.weatherinfo = weatherinfo; @H_301_4@10@H_301_4@ } @H_301_4@11@H_301_4@ }
class@H_301_4@ WeatherInfo { @H_301_4@private@H_301_4@ String city; @H_301_4@private@H_301_4@ String temp; @H_301_4@ 5@H_301_4@ private@H_301_4@ String time; @H_301_4@public@H_301_4@ String getCity() { @H_301_4@return@H_301_4@ city; @H_301_4@11@H_301_4@ 12@H_301_4@ void@H_301_4@ setCity(String city) { @H_301_4@this@H_301_4@.city = city; @H_301_4@14@H_301_4@ } @H_301_4@15@H_301_4@ 16@H_301_4@ public@H_301_4@ String getTemp() { @H_301_4@17@H_301_4@ return@H_301_4@ temp; @H_301_4@18@H_301_4@ } @H_301_4@19@H_301_4@ void@H_301_4@ setTemp(String temp) { @H_301_4@this@H_301_4@.temp = temp; @H_301_4@22@H_301_4@ } @H_301_4@23@H_301_4@ public@H_301_4@ String getTime() { @H_301_4@25@H_301_4@ return@H_301_4@ time; @H_301_4@26@H_301_4@ } @H_301_4@27@H_301_4@ 28@H_301_4@ void@H_301_4@ setTime(String time) { @H_301_4@29@H_301_4@ this@H_301_4@.time = time; @H_301_4@30@H_301_4@ } @H_301_4@31@H_301_4@ }

转自:http://www.cnblogs.com/freexiaoyu/p/3955137.html@H_301_4@

猜你在找的Json相关文章