Adrian
Adrian

Reputation: 77

Having trouble implementing JsonObjectRequest

I am having trouble implementing JsonObjectRequest for getting on JsonObject from the web. I am trying to create a new JsonObjectRequest :

  JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {

                        JSONObject ratesJsonObj = response.getJSONObject("rates");
                        String RON = ratesJsonObj.getString("RON");

                        Log.v("Currency", RON);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

But everything it's all squiggly an under highlited with red by android studio and it says

Error:(41, 47) error: reference to JsonObjectRequest is ambiguous, both constructor 
JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener)
in JsonObjectRequest and constructor 
JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener) 
in JsonObjectRequest match

Does anyone have any idea what's wrong?

Upvotes: 0

Views: 170

Answers (2)

Bedant
Bedant

Reputation: 326

Cast null to string and it will work. (String) null

Upvotes: 0

Oleg Khalidov
Oleg Khalidov

Reputation: 5268

try this way:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, (JSONObject) null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    JSONObject ratesJsonObj = response.getJSONObject("rates");
                    String RON = ratesJsonObj.getString("RON");

                    Log.v("Currency", RON);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

Upvotes: 2

Related Questions