1

I'm trying to send JSON parameter but server is receiving them as nulls values, i tried to request it from Postman and it works perfect, i don't know what the problem is with volley i followed the instructions here but it didn't make sense

here is my code

String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
    JSONObject jsonObject=new JSONObject();

    try {
        jsonObject.put("user_id","Test user name");
        jsonObject.put("user_password","test password");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    System.out.println(jsonObject.toString());

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  Request.Method.POST, url, jsonObject,
            new Response.Listener<JSONObject>() {



                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    System.out.println(error.toString());

                }
            });
    //add request to queue
    queue.add(jsonObjectRequest);
Community
  • 1
  • 1
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34

1 Answers1

1

I faced same problem, solved by overriding the getParams() method

Here is my login request using Volley.

private void loginRequeset() {
            showpDialog();

            StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
      hidepDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog
                    hidepDialog();
                }
            })

            {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> signup_param = new HashMap<String, String>();
                    signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
                    signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
                    return signup_param;
                }

            };

            // Adding request to request queue
            queue.getInstance().addToRequestQueue(jsonObjReq);
        }
Kathi
  • 1,061
  • 1
  • 16
  • 31
  • how did you send json object here ? – Basil Battikhi Mar 23 '16 at 15:15
  • @BasilBattikhi `return signup_param` – OneCricketeer Mar 23 '16 at 15:16
  • No need of _json object_ getParams() return Map. You can add your `jsonObject.put("user_id","Test user name"); jsonObject.put("user_password","test password");` to `signup_param.put("user_id","Test user name"); signup_param.put("user_password","test password");` – Kathi Mar 23 '16 at 15:20
  • you can use `getBody()` method as mention in this post [Use getBody method](http://stackoverflow.com/a/19838007/5955593) – Kathi Mar 26 '16 at 11:13