Compililation Error while using JsonObjectRequest
Asked Answered
A

3

9

I'm using mcxiaoke/android-volley library.Im getting compilation error as

Error:(77, 37) 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

this is my code.I dont know what is wrong. Any help appreciated

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            getRequestUrl(10),
            null,
            new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {


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

        }
    });
Acquisition answered 17/3, 2015 at 17:0 Comment(3)
you are passing a null value, pass a string or a JSONObjectJurisconsult
I think null value is accepted.Acquisition
yes null is accepted but the it dosent know know which constructor you are calling. cast the null to stringJurisconsult
J
29

Cast the null to string or JSONObject and it should work fine i think.

new JsonObjectRequest(Request.Method.GET,
            getRequestUrl(10),
            (String)null,
            new Response.Listener<JSONObject>()
Jurisconsult answered 17/3, 2015 at 17:11 Comment(1)
passing only null works in eclipse but on android studio i got that error and your solution solve my problem thanks:)Estrade
C
2

Bill Gates is right, there is no way for that class to know which constructor to use if you pass in null instead of the Object of type String or JSONObject it is expecting in one of the constructors otherwise you will get this ambiguous error, saying that the constructor has 2 matches.

Try:

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
        getRequestUrl(10),
        "",
        new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {


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

    }
});
Checkered answered 17/3, 2015 at 17:12 Comment(0)
W
0

You just used null reference.

new JsonObjectRequest(Request.Method.GET,
        getRequestUrl(10),
        (String)null,
        new Response.Listener<JSONObject>()

its work for me

Whitewing answered 2/2, 2016 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.