Volley 结合GSON或FastJson用法

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

自定义GSON类

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

自定义FastJson类

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

猜你在找的Json相关文章