Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.gson.Gson;
- import com.google.gson.JsonSyntaxException;
- import com.android.volley.AuthFailureError;
- 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 org.jetbrains.annotations.NotNull;
- import org.jetbrains.annotations.Nullable;
- import java.io.UnsupportedEncodingException;
- import java.util.Map;
- /**
- * Volley adapter for JSON requests that will be parsed into Java objects by Gson.
- * From https://gist.github.com/ficusk/5474673
- * @author Ficus Kirkpatrick
- */
- public class GsonRequest<T> extends Request<T> {
- private final Gson gson = new Gson();
- private final Class<T> clazz;
- @Nullable
- private final Map<String, String> headers;
- @Nullable
- private final Map<String, String> params;
- private final Listener<T> listener;
- private final int method;
- /**
- * Make a GET request and return a parsed object from JSON.
- *
- * @param url URL of the request to make
- * @param clazz Relevant class object, for Gson's reflection
- * @param data Map of request headers or parameters
- */
- public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> data,
- Listener<T> listener, ErrorListener errorListener) {
- super(method, url, errorListener);
- this.clazz = clazz;
- switch (method) {
- case Method.GET:
- this.headers = data;
- this.params = null;
- break;
- case Method.POST:
- this.headers = null;
- this.params = data;
- break;
- default:
- this.headers = null;
- this.params = null;
- }
- this.listener = listener;
- this.method = method;
- }
- @Nullable
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- return headers != null ? headers : super.getHeaders();
- }
- @Nullable
- @Override
- public Map<String, String> getParams() throws AuthFailureError {
- return params != null ? params : super.getParams();
- }
- @Override
- protected void deliverResponse(T response) {
- listener.onResponse(response);
- }
- @NotNull
- @Override
- public String getBodyContentType() {
- return "application/json; charset=" + getParamsEncoding();
- }
- @Override
- public byte[] getBody() throws AuthFailureError {
- if (this.method == Method.POST) {
- return new Gson().toJsonTree(this.params).toString().getBytes();
- }
- else
- return super.getBody();
- }
- @Override
- protected Response<T> parseNetworkResponse(@NotNull NetworkResponse response) {
- try {
- String json = new String(
- response.data, HttpHeaderParser.parseCharset(response.headers));
- return Response.success(
- gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
- } catch (UnsupportedEncodingException e) {
- return Response.error(new ParseError(e));
- } catch (JsonSyntaxException e) {
- return Response.error(new ParseError(e));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement