Volley 结合GSON或FastJson用法
自定义GSON类
1 public class GsonRequest<T> extends Request<T> { 2 private final Gson mGson = new Gson(); 3 private final Class<T> mClazz; 4 private final Listener<T> mListener; 5 private 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, Class<T> clazz, Map<String, String> headers, 12 Listener<T> listener, ErrorListener errorListener) { 13 super(method, url, errorListener); 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), 34 HttpHeaderParser.parseCacheHeaders(response)); 35 } catch (UnsupportedEncodingException e) { 36 return Response.error(new ParseError(e)); 37 } catch (JsonSyntaxException e) { 38 return Response.error(new ParseError(e)); 39 } 40 } 41 }
自定义FastJson类
1 public class FastJsonRequest<T> extends Request<T> { 2 private final Class<T> mClazz; 3 private final Listener<T> mListener; 4 private final Map<String, String> mHeaders; 5 6 public FastJsonRequest(String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { 7 this(Method.GET, url, clazz, null, listener, errorListener); 8 } 9 10 public FastJsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, 11 Listener<T> listener, ErrorListener errorListener) { 12 super(method, url, errorListener); 13 this.mClazz = clazz; 14 this.mHeaders = headers; 15 this.mListener = listener; 16 } 17 18 @Override 19 public Map<String, String> getHeaders() throws AuthFailureError { 20 return mHeaders != null ? mHeaders : super.getHeaders(); 21 } 22 23 @Override 24 protected void deliverResponse(T response) { 25 mListener.onResponse(response); 26 } 27 28 @Override 29 protected Response<T> parseNetworkResponse(NetworkResponse response) { 30 try { 31 String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 32 return Response.success(JSON.parseObject(json, mClazz), 33 HttpHeaderParser.parseCacheHeaders(response)); 34 } catch (UnsupportedEncodingException e) { 35 return Response.error(new ParseError(e)); 36 } 37 } 38 }
JAR下载
1 public class MainActivity extends Activity { 2 private Context context; 3 private String url="http://www.weather.com.cn/data/sk/101010100.html"; 4 private TextView text; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 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, 14 new Response.Listener<Weather>() { 15 16 @Override 17 public void onResponse(Weather weather) { 18 // TODO Auto-generated method stub 19 WeatherInfo weatherInfo = weather.getWeatherinfo(); 20 text.setText(weatherInfo.getCity()); 21 } 22 }, new Response.ErrorListener() { 23 24 @Override 25 public void onErrorResponse(VolleyError arg0) { 26 // TODO Auto-generated method stub 27 28 } 29 }); 30 mQueue.add(fastJson); 31 32 GsonRequest<Weather> gsonRequest=new GsonRequest<Weather>(url, Weather.class, 33 new Response.Listener<Weather>() { 34 35 @Override 36 public void onResponse(Weather weather) { 37 // TODO Auto-generated method stub 38 WeatherInfo weatherInfo = weather.getWeatherinfo(); 39 text.setText(text.getText()+weatherInfo.getTime()); 40 } 41 }, new Response.ErrorListener() { 42 43 @Override 44 public void onErrorResponse(VolleyError arg0) { 45 // TODO Auto-generated method stub 46 47 } 48 }); 49 mQueue.add(gsonRequest); 50 51 52 } 53 }
Model类
1 public class Weather { 2 private WeatherInfo weatherinfo; 3 4 public WeatherInfo getWeatherinfo() { 5 return weatherinfo; 6 } 7 8 public void setWeatherinfo(WeatherInfo weatherinfo) { 9 this.weatherinfo = weatherinfo; 10 } 11 }
1 public class WeatherInfo { 2 private String city; 3 4 private String temp; 5 6 private String time; 7 8 public String getCity() { 9 return city; 10 } 11 12 public void setCity(String city) { 13 this.city = city; 14 } 15 16 public String getTemp() { 17 return temp; 18 } 19 20 public void setTemp(String temp) { 21 this.temp = temp; 22 } 23 24 public String getTime() { 25 return time; 26 } 27 28 public void setTime(String time) { 29 this.time = time; 30 } 31 }