Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 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.ParseError; |
| 21 | import com.android.volley.Response; |
| 22 | import com.android.volley.Response.ErrorListener; |
| 23 | import com.android.volley.Response.Listener; |
| 24 | |
| 25 | import org.json.JSONArray; |
| 26 | import org.json.JSONException; |
| 27 | |
| 28 | import java.io.UnsupportedEncodingException; |
| 29 | |
| 30 | /** |
| 31 | * A request for retrieving a {@link JSONArray} response body at a given URL. |
| 32 | */ |
| 33 | public class JsonArrayRequest extends JsonRequest<JSONArray> { |
| 34 | |
| 35 | /** |
| 36 | * Creates a new request. |
| 37 | * @param url URL to fetch the JSON from |
| 38 | * @param listener Listener to receive the JSON response |
| 39 | * @param errorListener Error listener, or null to ignore errors. |
| 40 | */ |
| 41 | public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) { |
Jean-Baptiste Queru | e48f443 | 2012-11-07 07:54:36 -0800 | [diff] [blame] | 42 | super(Method.GET, url, null, listener, errorListener); |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Joris Bolsens | 6b30d94 | 2015-03-17 20:22:26 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Creates a new request. |
| 47 | * @param method the HTTP method to use |
| 48 | * @param url URL to fetch the JSON from |
| 49 | * @param jsonRequest A {@link JSONArray} to post with the request. Null is allowed and |
| 50 | * indicates no parameters will be posted along with request. |
| 51 | * @param listener Listener to receive the JSON response |
| 52 | * @param errorListener Error listener, or null to ignore errors. |
| 53 | */ |
| 54 | public JsonArrayRequest(int method, String url, JSONArray jsonRequest, |
| 55 | Listener<JSONArray> listener, ErrorListener errorListener) { |
| 56 | super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, |
| 57 | errorListener); |
| 58 | } |
| 59 | |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 60 | @Override |
| 61 | protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { |
| 62 | try { |
Zdeněk Kořán | 6bafd7d | 2015-01-20 22:45:25 +0100 | [diff] [blame] | 63 | String jsonString = new String(response.data, |
| 64 | HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 65 | return Response.success(new JSONArray(jsonString), |
| 66 | HttpHeaderParser.parseCacheHeaders(response)); |
| 67 | } catch (UnsupportedEncodingException e) { |
| 68 | return Response.error(new ParseError(e)); |
| 69 | } catch (JSONException je) { |
| 70 | return Response.error(new ParseError(je)); |
| 71 | } |
| 72 | } |
| 73 | } |