Chor Sipahi
Chor Sipahi

Reputation: 546

Android Class JsonRequest and Subclasses JsonRequestArray and JsonRequestObject

May be my question is bit naive, as I am pretty new to Andorid. I am trying to use the class JsonRequestArray to send a GET request. I want to send some parameters with the request. I found some of the answers saying to make customRequest. However I want to use JsonRequestArray class only. From Androids tutorial its seems that we need to pass something to constructor in place of null here:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()

But its not clear that what should be the format of the paramater to set parameters. I tried to search for constructor of the class's JSONObjectArray but couldn't find it. Any help would be appreciated.

Upvotes: 1

Views: 439

Answers (2)

BNK
BNK

Reputation: 24114

IMO, you can refer to the following JavaDoc content:

    /**
     * Creates a new request.
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param jsonRequest A {@link JSONObject} 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 JsonObjectRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

If you use mcxiaoke's Volley instead of Google's one, you will then have other constructors, such as:

    /**
     * Creates a new request.
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param requestBody A {@link String} 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 JsonObjectRequest(int method, String url, String requestBody,
                             Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, requestBody, listener,
                errorListener);
    }

Upvotes: 0

g90
g90

Reputation: 498

unfortunately the android-volley does not have a documentation source or atleast not one I could find. So I went to the source code at this link.

The following is one of the constructor methods and you can pass the parameters in jsonRequest.

JsonArrayRequest(int method, String url, JSONArray jsonRequest,
                        Listener<JSONArray> listener, ErrorListener errorListener)

Let me know if you need any more help with this.

Upvotes: 1

Related Questions