Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / com / android / volley / toolbox / StringRequest.java
blob6b3dfcf8ab9bd0d914e9e13cac335433342d103b
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.Request;
21 import com.android.volley.Response;
22 import com.android.volley.Response.ErrorListener;
23 import com.android.volley.Response.Listener;
25 import java.io.UnsupportedEncodingException;
27 /**
28 * A canned request for retrieving the response body at a given URL as a String.
30 public class StringRequest extends Request<String> {
31 private final Listener<String> mListener;
33 /**
34 * Creates a new request with the given method.
36 * @param method the request {@link Method} to use
37 * @param url URL to fetch the string at
38 * @param listener Listener to receive the String response
39 * @param errorListener Error listener, or null to ignore errors
41 public StringRequest(int method, String url, Listener<String> listener,
42 ErrorListener errorListener) {
43 super(method, url, errorListener);
44 mListener = listener;
47 /**
48 * Creates a new GET request.
50 * @param url URL to fetch the string at
51 * @param listener Listener to receive the String response
52 * @param errorListener Error listener, or null to ignore errors
54 public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
55 this(Method.GET, url, listener, errorListener);
58 @Override
59 protected void deliverResponse(String response) {
60 mListener.onResponse(response);
63 @Override
64 protected Response<String> parseNetworkResponse(NetworkResponse response) {
65 String parsed;
66 try {
67 parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
68 } catch (UnsupportedEncodingException e) {
69 parsed = new String(response.data);
71 return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));