Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / com / android / volley / toolbox / JsonObjectRequest.java
blob299189884d3f87ace5b307e1966cf7cdac6349fe
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.volley.toolbox;
19 import com.android.volley.NetworkResponse;
20 import com.android.volley.ParseError;
21 import com.android.volley.Response;
22 import com.android.volley.Response.ErrorListener;
23 import com.android.volley.Response.Listener;
25 import org.json.JSONException;
26 import org.json.JSONObject;
28 import java.io.UnsupportedEncodingException;
30 /**
31 * A request for retrieving a {@link JSONObject} response body at a given URL, allowing for an
32 * optional {@link JSONObject} to be passed in as part of the request body.
34 public class JsonObjectRequest extends JsonRequest<JSONObject> {
36 /**
37 * Creates a new request.
38 * @param method the HTTP method to use
39 * @param url URL to fetch the JSON from
40 * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
41 * indicates no parameters will be posted along with request.
42 * @param listener Listener to receive the JSON response
43 * @param errorListener Error listener, or null to ignore errors.
45 public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
46 Listener<JSONObject> listener, ErrorListener errorListener) {
47 super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
48 errorListener);
51 /**
52 * Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is
53 * <code>null</code>, <code>POST</code> otherwise.
55 * @see #JsonObjectRequest(int, String, JSONObject, Listener, ErrorListener)
57 public JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,
58 ErrorListener errorListener) {
59 this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
60 listener, errorListener);
63 @Override
64 protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
65 try {
66 String jsonString = new String(response.data,
67 HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
68 return Response.success(new JSONObject(jsonString),
69 HttpHeaderParser.parseCacheHeaders(response));
70 } catch (UnsupportedEncodingException e) {
71 return Response.error(new ParseError(e));
72 } catch (JSONException je) {
73 return Response.error(new ParseError(je));