文章中继承了Volley的 request,并在请求返回的时候在异步的时候就转了JSON对象
代码如下,包含了POST与GET
private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", "utf-8"); private final String requestBody; private final Gson gson = new Gson(); private final Class<T> clazz; private final Map<String, String> headers; private final Response.Listener<T> listener; private boolean requestBodyGzipEnable = true;
构造函数
public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) { this(Method.GET, url, null, clazz, listener, errorListener); } public GsonRequest(int method, String url, String requestBody, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) { this(method, url, null, requestBody, clazz, listener, errorListener); } public GsonRequest(int method, String url, Map<String, String> headers, String requestBody, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) { super(method, url, errorListener); this.clazz = clazz; this.headers = headers; this.listener = listener; this.requestBody = requestBody; }
是否需要GZip压缩(项目中需要,可根据实际情况来添加是否需要) (Gzip这个类就不贴出来了)
public void setRequestBodyGzipEnable(boolean enable){ requestBodyGzipEnable = enable; }
@Override public Map<String, String> getHeaders() throws AuthFailureError { return headers != null ? headers : super.getHeaders(); }
@Override protected void deliverResponse(T response) { listener.onResponse(response); }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
//就在这里,返回成功的时候直接转成了JSON对象, 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)); } }
@Override public String getBodyContentType() { return PROTOCOL_CONTENT_TYPE; } @Override public byte[] getBody() { try { if(requestBody == null){ return null; } return this.requestBodyGzipEnable ? Gzip.gzip(requestBody) : requestBody.getBytes(); } catch (Exception e) { Logger.e(TAG, e.toString(), e); return null; } }
还有一个Networking的类
public class Networking { private RequestQueue mQueue; private static class SingletonHolder { private static final Networking networking = new Networking(); } protected Networking() { mQueue = Volley.newRequestQueue(AppEnv.getApplicationContext()); } private static RequestQueue getRequestQueue() { return SingletonHolder.networking.mQueue; } public static <T> Request<T> add(Request<T> request) { RetryPolicy retryPolicy = new DefaultRetryPolicy(Configure.TIME_OUT, Configure.RETRY_COUNT, 1); return add(request, retryPolicy); } public static <T> Request<T> add(Request<T> request, @NonNull RetryPolicy retryPolicy) { request.setRetryPolicy(retryPolicy); return getRequestQueue().add(request); } public static void cancelAll(final Object tag) { getRequestQueue().cancelAll(tag); } public static void cancelAll(RequestQueue.RequestFilter filter) { getRequestQueue().cancelAll(filter); }
使用的时候如下
public static void getShippingDetails(final HttpCallback<ShippingDetailResponse> callback){ final String url = UrlConstants.ShippingDetailURL; Logger.d(TAG, "url =" + url); GsonRequest<ShippingDetailResponse> request = new GsonRequest<ShippingDetailResponse>(url, ShippingDetailResponse.class, new Response.Listener<ShippingDetailResponse>() { @Override public void onResponse(ShippingDetailResponse response) { if(callback!=null) callback.onSuccess(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError err) { Logger.e(TAG, "onError: " + url); if (callback != null) { callback.onError(err.toString(), err); } } }); Networking.add(request); }
HttpCallback //是自己写的一个接口
public interface HttpCallback<T> { void onSuccess(T response); void onError(String errMsg, Throwable e); }
使用起来还是蛮方便的 ,POST的就不再贴代码了!,欢迎各位看官指点,多谢!