7

I have been fiddling with this for a few days now. Couldn't get it right. Android studio won't let me compile it with this error. So, I have this application where I have two tabs and two fragments. One fragment is called new, and that fragment fetches json. But I couldn't get to do it properly. I have uploaded a picture of how the error looks like, and the class files. Can you please help me out?

Error: "Cannot resolve constructor JsonObjectRequest(int, java.lang.String, null.......)

The error

new_fragment.java

public class new_fragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() !=null){
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        volleySingleton = VolleySingleton.getsInstance();
        requestQueue = volleySingleton.getRequestQueue();
        RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue.add(request);
    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedIntanceState) {
        setHasOptionsMenu(true);
        View layout = inflater.inflate(R.layout.new_fragment, container, false);
        return layout;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.ref_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.refreshico:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

VolleySingleton

public class VolleySingleton {
    private static VolleySingleton sInstance = null;
    private ImageLoader mImageLoader;
    private RequestQueue mRequestQueue;
    private VolleySingleton(){
        mRequestQueue = Volley.newRequestQueue(appClass.getAppContext());
        mImageLoader = new ImageLoader(mRequestQueue,new ImageLoader.ImageCache() {
            private LruCache<String, Bitmap> cache = new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024)/8);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static VolleySingleton getsInstance(){
        if(sInstance==null){
            sInstance = new VolleySingleton();
        }
        return sInstance;
    }

    public RequestQueue getRequestQueue(){
        return mRequestQueue;
    }
    public ImageLoader getImageLoader(){
        return mImageLoader;
    }


}
dashhund
  • 322
  • 3
  • 17
Richie
  • 85
  • 1
  • 1
  • 4

6 Answers6

24

cast (String)null.

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",(String)null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
rKrishna
  • 1,399
  • 12
  • 22
9

new JsonObjectRequest takes 3rd Parameter as String requestBody

cast null to String

or you can make a null string like String rBody = null; and then pass rBody as 3rd Parameter

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                "http://10.0.8.152/json/new.json", (String) null, // here
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
requestQueue.add(request);
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • @rogertthatcode I had the same problem. And the solution u given solved. Why should we give like this? Could you pls explain. And/Or why we need to cast to String? – Karthikeyan Ve Jun 23 '15 at 07:22
  • @KarthikeyanVe i found [this post](http://stackoverflow.com/questions/315846/why-null-cast) helpful regarding the same. It talks about `varargs` function. I will do some more search also – Kushal Sharma Jun 23 '15 at 08:26
2

For everyone trying to implement this right now: Google changed the jsonObjectRequest constructor: now you don't suppy the request type anymore, instead you supply firstly the url and then null as second parameter if you want a get request. e.g.:

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.aaa.com/getJSON/dummyMeldung.json", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("meldung");
                            for (int i = 0; i <= jsonArray.length(); i++) {
                                JSONObject meldung = jsonArray.getJSONObject(i);
                                String comment = meldung.getString("comment");
                                Log.d("ausgabe", comment);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

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

                    }
                });
AlexeyGy
  • 306
  • 3
  • 9
1

Well, i see the situation, and it happends because as your error say, the constructor doesnt exists.

If you are using the JsonObjectRequest as default, and you want to consume an Get method, you dont have to send the null parameter, you just should send of this way:

Change this:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    requestQueue.add(request);

For this:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    requestQueue.add(request);

As you can see, i just remove the parameter for the JsonObject, because the method is Get, and there is an constructor thats accept that you dont send JsonObject.

Another solution is to make your own JsonObjectRequest, and custom it to accept that kind of values.

Regards.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29
0

There is a ambiguous Constructor, for solve this, use this code:

    JSONObject jsonObject = null;

    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.GET, "Your url", jsonObject,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                }
            }, 
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                }
         });
javad
  • 833
  • 14
  • 37
0

The problem as to do with the version of volley. On your build.gradle app file you should have

dependencies {
......
compile 'com.mcxiaoke.volley:library:1.0.0'
.....
}
paulomaranhao
  • 19
  • 1
  • 6