自定义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类