Created
January 20, 2015 18:09
-
-
Save guigmaster/2680fc42a63ee435d20c to your computer and use it in GitHub Desktop.
GsonRequest a Volley adapter for JSON requests using Google Gson Lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GsonRequest<T> extends Request<T> { | |
private Gson gson = new Gson(); | |
private Class<T> model; | |
private Map<String, String> params; | |
private Map<String, String> headers; | |
private Listener listener; | |
public GsonRequest(int method, String url, Class<T> model, Map<String, String> params, Map<String, String> headers, | |
Listener<T> listener, ErrorListener errorListener) { | |
super(method, url, errorListener); | |
this.model = model; | |
this.params = (params != null) ? params : new HashMap<String, String>(); | |
this.headers = (headers != null) ? headers : new HashMap<String, String>(); | |
this.listener = listener; | |
} | |
public GsonRequest(int method, String url, Class<T> model, Map<String, String> params, | |
Listener<T> listener, ErrorListener errorListener) { | |
super(method, url, errorListener); | |
this.model = model; | |
this.params = (params != null) ? params : new HashMap<String, String>(); | |
this.headers = new HashMap<String, String>(); | |
this.listener = listener; | |
} | |
public GsonRequest(int method, String url, Class<T> model, Listener<T> listener, ErrorListener errorListener) { | |
super(method, url, errorListener); | |
this.model = model; | |
this.params = new HashMap<String, String>(); | |
this.headers = new HashMap<String, String>(); | |
this.listener = listener; | |
} | |
@Override | |
public Map<String, String> getParams() throws AuthFailureError { | |
return params ; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
return headers; | |
} | |
@Override | |
public byte[] getBody() throws AuthFailureError { | |
Map<String, String> params = this.getParams(); | |
if (params != null && params.size() > 0) { | |
return encodeParameters(params, getParamsEncoding()); | |
} | |
return null; | |
} | |
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) { | |
StringBuilder encodedParams = new StringBuilder(); | |
try { | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding)); | |
encodedParams.append('='); | |
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding)); | |
encodedParams.append('&'); | |
} | |
return encodedParams.toString().getBytes(paramsEncoding); | |
} catch (UnsupportedEncodingException uee) { | |
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee); | |
} | |
} | |
@Override | |
protected void deliverResponse(T response) { | |
this.listener.onResponse(response); | |
} | |
@Override | |
protected Response<T> parseNetworkResponse(NetworkResponse response) { | |
try { | |
String json = new String( | |
response.data, | |
HttpHeaderParser.parseCharset(response.headers)); | |
return Response.success( | |
gson.fromJson(json, model), | |
HttpHeaderParser.parseCacheHeaders(response)); | |
} catch (UnsupportedEncodingException e) { | |
return Response.error(new ParseError(e)); | |
} catch (JsonSyntaxException e) { | |
return Response.error(new ParseError(e)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment