blob: f11ac14e43c74d211cf2adab212e5e91052b5401 [file] [log] [blame]
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -08001/*
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
17package com.android.volley.toolbox;
18
19import com.android.volley.NetworkResponse;
20import com.android.volley.Request;
21import com.android.volley.Response;
22import com.android.volley.Response.ErrorListener;
23import com.android.volley.Response.Listener;
24import com.android.volley.VolleyLog;
25
26import 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 */
34public abstract class JsonRequest<T> extends Request<T> {
35 /** Charset for request. */
36 private 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 private final Listener<T> mListener;
43 private final String mRequestBody;
44
Jean-Baptiste Querue48f4432012-11-07 07:54:36 -080045 /**
46 * Deprecated constructor for a JsonRequest which defaults to GET unless {@link #getPostBody()}
47 * or {@link #getPostParams()} is overridden (which defaults to POST).
48 *
49 * @deprecated Use {@link #JsonRequest(int, String, String, Listener, ErrorListener)}.
50 */
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080051 public JsonRequest(String url, String requestBody, Listener<T> listener,
52 ErrorListener errorListener) {
Jean-Baptiste Querue48f4432012-11-07 07:54:36 -080053 this(Method.DEPRECATED_GET_OR_POST, url, requestBody, listener, errorListener);
54 }
55
56 public JsonRequest(int method, String url, String requestBody, Listener<T> listener,
57 ErrorListener errorListener) {
58 super(method, url, errorListener);
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080059 mListener = listener;
60 mRequestBody = requestBody;
61 }
62
63 @Override
64 protected void deliverResponse(T response) {
65 mListener.onResponse(response);
66 }
67
68 @Override
69 abstract protected Response<T> parseNetworkResponse(NetworkResponse response);
70
Jean-Baptiste Querue48f4432012-11-07 07:54:36 -080071 /**
72 * @deprecated Use {@link #getBodyContentType()}.
73 */
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080074 @Override
75 public String getPostBodyContentType() {
Jean-Baptiste Querue48f4432012-11-07 07:54:36 -080076 return getBodyContentType();
77 }
78
79 /**
80 * @deprecated Use {@link #getBody()}.
81 */
82 @Override
83 public byte[] getPostBody() {
84 return getBody();
85 }
86
87 @Override
88 public String getBodyContentType() {
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080089 return PROTOCOL_CONTENT_TYPE;
90 }
91
92 @Override
Jean-Baptiste Querue48f4432012-11-07 07:54:36 -080093 public byte[] getBody() {
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080094 try {
95 return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
96 } catch (UnsupportedEncodingException uee) {
97 VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
98 mRequestBody, PROTOCOL_CHARSET);
99 return null;
100 }
101 }
102}