Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 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.Request;
21
import com.android.volley.Response;
22
import com.android.volley.Response.ErrorListener;
23
import com.android.volley.Response.Listener;
24
import com.android.volley.VolleyLog;
25
 
26
import java.io.UnsupportedEncodingException;
27
 
28
/**
29
 * A request for retrieving a T type response body at a given URL that also
30
 * optionally sends along a JSON body in the request specified.
31
 *
32
 * @param <T> JSON type of response expected
33
 */
34
public abstract class JsonRequest<T> extends Request<T> {
35
    /** Default charset for JSON request. */
36
    protected static final String PROTOCOL_CHARSET = "utf-8";
37
 
38
    /** Content type for request. */
39
    private static final String PROTOCOL_CONTENT_TYPE =
40
        String.format("application/json; charset=%s", PROTOCOL_CHARSET);
41
 
42
    private final Listener<T> mListener;
43
    private final String mRequestBody;
44
 
45
    /**
46
     * Deprecated constructor for a JsonRequest which defaults to GET unless {@link #getPostBody()}
47
     * or {@link #getPostParams()} is overridden (which defaults to POST).
48
     *
49
     * @deprecated Use {@link #JsonRequest(int, String, String, Listener, ErrorListener)}.
50
     */
51
    public JsonRequest(String url, String requestBody, Listener<T> listener,
52
            ErrorListener errorListener) {
53
        this(Method.DEPRECATED_GET_OR_POST, url, requestBody, listener, errorListener);
54
    }
55
 
56
    public JsonRequest(int method, String url, String requestBody, Listener<T> listener,
57
            ErrorListener errorListener) {
58
        super(method, url, errorListener);
59
        mListener = listener;
60
        mRequestBody = requestBody;
61
    }
62
 
63
    @Override
64
    protected void deliverResponse(T response) {
65
        mListener.onResponse(response);
66
    }
67
 
68
    @Override
69
    abstract protected Response<T> parseNetworkResponse(NetworkResponse response);
70
 
71
    /**
72
     * @deprecated Use {@link #getBodyContentType()}.
73
     */
74
    @Override
75
    public String getPostBodyContentType() {
76
        return getBodyContentType();
77
    }
78
 
79
    /**
80
     * @deprecated Use {@link #getBody()}.
81
     */
82
    @Override
83
    public byte[] getPostBody() {
84
        return getBody();
85
    }
86
 
87
    @Override
88
    public String getBodyContentType() {
89
        return PROTOCOL_CONTENT_TYPE;
90
    }
91
 
92
    @Override
93
    public byte[] getBody() {
94
        try {
95
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
96
        } catch (UnsupportedEncodingException uee) {
97
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
98
                    mRequestBody, PROTOCOL_CHARSET);
99
            return null;
100
        }
101
    }
102
}