Back to project page ChattyOwl.
The source code is released under:
MIT License
If you think the Android project ChattyOwl listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.znupy.chattyowl.network; //www . j a v a 2 s. co m import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; import java.io.UnsupportedEncodingException; import java.util.Map; public class GsonRequest<T> extends Request<T> { private final Gson mGson; private final Class<T> mClazz; private final Listener<T> mListener; private Map<String, String> mParams; public GsonRequest(int method, String url, Map<String, String> params, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { super(method, url, errorListener); this.mClazz = clazz; this.mListener = listener; this.mParams = params; mGson = new Gson(); } public GsonRequest(int method, String url, Map<String, String> params, Class<T> clazz, Listener<T> listener, ErrorListener errorListener, Gson gson) { super(method, url, errorListener); this.mClazz = clazz; this.mListener = listener; this.mParams = params; mGson = gson; } protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return mParams; }; @Override protected void deliverResponse(T response) { mListener.onResponse(response); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(mGson.fromJson(json, mClazz), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } } }