Advertisement
Guest User

Untitled

a guest
Mar 18th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. import com.google.gson.Gson;
  2. import com.google.gson.JsonSyntaxException;
  3.  
  4. import com.android.volley.AuthFailureError;
  5. import com.android.volley.NetworkResponse;
  6. import com.android.volley.ParseError;
  7. import com.android.volley.Request;
  8. import com.android.volley.Response;
  9. import com.android.volley.Response.ErrorListener;
  10. import com.android.volley.Response.Listener;
  11. import com.android.volley.toolbox.HttpHeaderParser;
  12.  
  13. import org.jetbrains.annotations.NotNull;
  14. import org.jetbrains.annotations.Nullable;
  15.  
  16. import java.io.UnsupportedEncodingException;
  17. import java.util.Map;
  18.  
  19. /**
  20.  * Volley adapter for JSON requests that will be parsed into Java objects by Gson.
  21.  * From https://gist.github.com/ficusk/5474673
  22.  * @author Ficus Kirkpatrick
  23.  */
  24. public class GsonRequest<T> extends Request<T> {
  25.     private final Gson gson = new Gson();
  26.     private final Class<T> clazz;
  27.     @Nullable
  28.     private final Map<String, String> headers;
  29.     @Nullable
  30.     private final Map<String, String> params;
  31.     private final Listener<T> listener;
  32.     private final int method;
  33.  
  34.     /**
  35.      * Make a GET request and return a parsed object from JSON.
  36.      *
  37.      * @param url URL of the request to make
  38.      * @param clazz Relevant class object, for Gson's reflection
  39.      * @param data Map of request headers or parameters
  40.      */
  41.     public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> data,
  42.                        Listener<T> listener, ErrorListener errorListener) {
  43.         super(method, url, errorListener);
  44.         this.clazz = clazz;
  45.  
  46.         switch (method) {
  47.             case Method.GET:
  48.                 this.headers = data;
  49.                 this.params = null;
  50.                 break;
  51.  
  52.             case Method.POST:
  53.                 this.headers = null;
  54.                 this.params = data;
  55.                 break;
  56.  
  57.             default:
  58.                 this.headers = null;
  59.                 this.params = null;
  60.         }
  61.  
  62.         this.listener = listener;
  63.         this.method = method;
  64.     }
  65.  
  66.     @Nullable
  67.     @Override
  68.     public Map<String, String> getHeaders() throws AuthFailureError {
  69.         return headers != null ? headers : super.getHeaders();
  70.     }
  71.  
  72.     @Nullable
  73.     @Override
  74.     public Map<String, String> getParams() throws AuthFailureError {
  75.         return params != null ? params : super.getParams();
  76.     }
  77.  
  78.     @Override
  79.     protected void deliverResponse(T response) {
  80.         listener.onResponse(response);
  81.     }
  82.  
  83.     @NotNull
  84.     @Override
  85.     public String getBodyContentType() {
  86.         return "application/json; charset=" + getParamsEncoding();
  87.     }
  88.  
  89.     @Override
  90.     public byte[] getBody() throws AuthFailureError {
  91.         if (this.method == Method.POST) {
  92.             return new Gson().toJsonTree(this.params).toString().getBytes();
  93.         }
  94.         else
  95.             return super.getBody();
  96.     }
  97.  
  98.     @Override
  99.     protected Response<T> parseNetworkResponse(@NotNull NetworkResponse response) {
  100.         try {
  101.             String json = new String(
  102.                     response.data, HttpHeaderParser.parseCharset(response.headers));
  103.             return Response.success(
  104.                     gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
  105.         } catch (UnsupportedEncodingException e) {
  106.             return Response.error(new ParseError(e));
  107.         } catch (JsonSyntaxException e) {
  108.             return Response.error(new ParseError(e));
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement