1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.volley.toolbox; 18 19 import com.android.volley.NetworkResponse; 20 import com.android.volley.Request; 21 import com.android.volley.Response; 22 import com.android.volley.Response.ErrorListener; 23 import com.android.volley.Response.Listener; 24 import com.android.volley.VolleyLog; 25 26 import java.io.UnsupportedEncodingException; 27 28 /** 29 * A request for retrieving a T type response body at a given URL that also 30 * optionally sends along a JSON body in the request specified. 31 * 32 * @param <T> JSON type of response expected 33 */ 34 public abstract class JsonRequest<T> extends Request<T> { 35 /** Default charset for JSON request. */ 36 protected static final String PROTOCOL_CHARSET = "utf-8"; 37 38 /** Content type for request. */ 39 private static final String PROTOCOL_CONTENT_TYPE = 40 String.format("application/json; charset=%s", PROTOCOL_CHARSET); 41 42 /** Lock to guard mListener as it is cleared on cancel() and read on delivery. */ 43 private final Object mLock = new Object(); 44 45 // @GuardedBy("mLock") 46 private Listener<T> mListener; 47 private final String mRequestBody; 48 49 /** 50 * Deprecated constructor for a JsonRequest which defaults to GET unless {@link #getPostBody()} 51 * or {@link #getPostParams()} is overridden (which defaults to POST). 52 * 53 * @deprecated Use {@link #JsonRequest(int, String, String, Listener, ErrorListener)}. 54 */ 55 @Deprecated 56 public JsonRequest(String url, String requestBody, Listener<T> listener, 57 ErrorListener errorListener) { 58 this(Method.DEPRECATED_GET_OR_POST, url, requestBody, listener, errorListener); 59 } 60 61 public JsonRequest(int method, String url, String requestBody, Listener<T> listener, 62 ErrorListener errorListener) { 63 super(method, url, errorListener); 64 mListener = listener; 65 mRequestBody = requestBody; 66 } 67 68 @Override 69 public void cancel() { 70 super.cancel(); 71 synchronized (mLock) { 72 mListener = null; 73 } 74 } 75 76 @Override 77 protected void deliverResponse(T response) { 78 Response.Listener<T> listener; 79 synchronized (mLock) { 80 listener = mListener; 81 } 82 if (listener != null) { 83 listener.onResponse(response); 84 } 85 } 86 87 @Override 88 abstract protected Response<T> parseNetworkResponse(NetworkResponse response); 89 90 /** 91 * @deprecated Use {@link #getBodyContentType()}. 92 */ 93 @Deprecated 94 @Override 95 public String getPostBodyContentType() { 96 return getBodyContentType(); 97 } 98 99 /** 100 * @deprecated Use {@link #getBody()}. 101 */ 102 @Deprecated 103 @Override 104 public byte[] getPostBody() { 105 return getBody(); 106 } 107 108 @Override 109 public String getBodyContentType() { 110 return PROTOCOL_CONTENT_TYPE; 111 } 112 113 @Override 114 public byte[] getBody() { 115 try { 116 return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET); 117 } catch (UnsupportedEncodingException uee) { 118 VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 119 mRequestBody, PROTOCOL_CHARSET); 120 return null; 121 } 122 } 123 } 124