源代码其实也非常的简单...
package com.android.volley.toolbox; import com.android.volley.NetworkResponse; 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.VolleyLog; import java.io.UnsupportedEncodingException; JsonRequest<T> extends Request<T> { String PROTOCOL_CHARSET = "utf-8"; String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET); Listener<T> mListener; String mRequestBody; //请求实体... JsonRequest(String url, String requestBody, Listener<T> listener, ErrorListener errorListener) { this(Method.DEPRECATED_GET_OR_POST, url, requestBody, listener, errorListener); } JsonRequest(int method, String url, String requestBody, Listener<T> listener, ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; mRequestBody = requestBody; } //请求的分发... @Override protected void deliverResponse(T response) { mListener.onResponse(response); } //解析请求的抽象方法,使JsonArrayRequest和JsonObjectRequest去实现.. @Override abstract protected Response<T> parseNetworkResponse(NetworkResponse response); //获取Post请求中实体内容的类型.. @Override public String getPostBodyContentType() { return getBodyContentType(); } //获取Post请求的实体部分... @Override public byte[] getPostBody() { return getBody(); } //指定内容类型... @Override public String getBodyContentType() { return PROTOCOL_CONTENT_TYPE; } //获取实体内容的具体过程... @Override public byte[] getBody() { try { return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET); } catch (UnsupportedEncodingException uee) { VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, PROTOCOL_CHARSET); return null; } } }