Search code examples
androidandroid-volleyjsonobjectrequest

Android Volley JsonObjectRequest format: http://localhost:8080/xy?param1=1&param2=2


I'm trying to make a Volley JsonObjectRequest (GET) sending Parameters in the following format:

http://localhost:8080/xy?param1=1&param2=2

My Problem is, I should get a Response-Code 200 (OK), if param1 is "1" and param2 is "2". But I always get the wrong Response Code. So I think, the request is sending in the wrong format.

Map<String, String> params = new HashMap();
            params.put("param1", "1");
            params.put("param2", "2");
            JsonObjectRequest jsObjRequest = new JsonObjectRequest
                    (Request.Method.GET, "http://localhost:8080/xy", new JSONObject(params), new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {

                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // TODO Auto-generated method stub

                        }
                    });

            // Access the RequestQueue through your singleton class.
            QueueSingleton.getInstance(LoginActivity.this).addToRequestQueue(jsObjRequest);

Thanks!


Solution

  • Right now, you're providing your JsonObject(params) as the body of the request, which is incorrect. I don't think Volley will append your provided JSON object to your URL for you on a GET request...so you need to do that yourself.

    Get rid of adding the post body, and append the params manually on the URL using Uri.Builder.appendQueryParameter(key, value).