Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
                    }
                });
like image 205
Santhosh Avatar asked Jun 14 '26 09:06

Santhosh


1 Answers

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!

like image 178
BNK Avatar answered Jun 16 '26 23:06

BNK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!