Santhosh
Santhosh

Reputation: 11788

Android Volley: Cannot resolve Constructor jsonobjectrequest

I am trying to use volley to fetch JSON and parse. But android studio says cannot resolve constructor jsonobjetrequest. I am not able to understand what is the mistake. The following is the code.

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

                    @Override
                    public void onResponse(JSONObject response) {
                        try{
                            JSONArray routes = response.getJSONArray("routes");
                        }
                        catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

Upvotes: 3

Views: 4047

Answers (1)

BNK
BNK

Reputation: 24114

Because your project uses mcxiaoke's volley, in which there are two following constructors:

public JsonObjectRequest(int method, String url, String requestBody,
                             Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, requestBody, listener,
                errorListener);
    }

public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

So if you pass null, there is no way for the class to know which constructor to be used.

As commented, you can either remove Request.Method.GET, or remove null, or casting such as (String)null or (JSONObject)null.

P/S: if your project uses Google's official volley, the constructor in your question is correct.

Hope this helps!

Upvotes: 3

Related Questions