blob: e932a61c341a63e2c7b302e6a97606d45e3825e6 [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.ParseError;
21import com.android.volley.Response;
22import com.android.volley.Response.ErrorListener;
23import com.android.volley.Response.Listener;
24
25import org.json.JSONArray;
26import org.json.JSONException;
27
28import java.io.UnsupportedEncodingException;
29
30/**
31 * A request for retrieving a {@link JSONArray} response body at a given URL.
32 */
33public 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 Querue48f4432012-11-07 07:54:36 -080042 super(Method.GET, url, null, listener, errorListener);
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080043 }
44
45 @Override
46 protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
47 try {
Zdeněk Kořán6bafd7d2015-01-20 22:45:25 +010048 String jsonString = new String(response.data,
49 HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080050 return Response.success(new JSONArray(jsonString),
51 HttpHeaderParser.parseCacheHeaders(response));
52 } catch (UnsupportedEncodingException e) {
53 return Response.error(new ParseError(e));
54 } catch (JSONException je) {
55 return Response.error(new ParseError(je));
56 }
57 }
58}