From 9c62e4bb272e5c5b3d670bc522eec07bb1f893be Mon Sep 17 00:00:00 2001 From: Przemek Grondek Date: Sat, 12 Nov 2016 01:33:32 +0100 Subject: [PATCH] Move JsonObjectArrayRequest to volley-toolbox --- build.gradle | 5 +- .../volley/JsonObjectArrayRequest.java | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/main/java/info/nerull7/volley/JsonObjectArrayRequest.java diff --git a/build.gradle b/build.gradle index 1e71c2b..c662439 100644 --- a/build.gradle +++ b/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 21 targetSdkVersion 25 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "0.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -27,5 +27,6 @@ dependencies { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' + compile 'com.android.volley:volley:1.0.0' testCompile 'junit:junit:4.12' } diff --git a/src/main/java/info/nerull7/volley/JsonObjectArrayRequest.java b/src/main/java/info/nerull7/volley/JsonObjectArrayRequest.java new file mode 100644 index 0000000..f448d38 --- /dev/null +++ b/src/main/java/info/nerull7/volley/JsonObjectArrayRequest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info.nerull7.volley; + +import com.android.volley.NetworkResponse; +import com.android.volley.ParseError; +import com.android.volley.Response; +import com.android.volley.Response.ErrorListener; +import com.android.volley.Response.Listener; +import com.android.volley.toolbox.HttpHeaderParser; +import com.android.volley.toolbox.JsonRequest; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.UnsupportedEncodingException; + +/** + * A request for retrieving a {@link JSONArray} response body at a given URL. + */ +public class JsonObjectArrayRequest extends JsonRequest { + + /** + * Creates a new request. + * @param url URL to fetch the JSON from + * @param listener Listener to receive the JSON response + * @param errorListener Error listener, or null to ignore errors. + */ + public JsonObjectArrayRequest(String url, Listener listener, ErrorListener errorListener) { + super(Method.GET, url, null, listener, errorListener); + } + + /** + * Creates a new request. + * @param method the HTTP method to use + * @param url URL to fetch the JSON from + * @param jsonRequest A {@link JSONArray} to post with the request. Null is allowed and + * indicates no parameters will be posted along with request. + * @param listener Listener to receive the JSON response + * @param errorListener Error listener, or null to ignore errors. + */ + public JsonObjectArrayRequest(int method, String url, JSONObject jsonRequest, + Listener listener, ErrorListener errorListener) { + super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, + errorListener); + } + + @Override + protected Response parseNetworkResponse(NetworkResponse response) { + try { + String jsonString = new String(response.data, + HttpHeaderParser.parseCharset(response.headers, JsonRequest.PROTOCOL_CHARSET)); + return Response.success(new JSONArray(jsonString), + HttpHeaderParser.parseCacheHeaders(response)); + } catch (UnsupportedEncodingException e) { + return Response.error(new ParseError(e)); + } catch (JSONException je) { + return Response.error(new ParseError(je)); + } + } +}