-1

In my project, I want to send a request with Volley map inside map.
But I'm getting errors, 406 errors.

Here is my request:

StringRequest myReq = new StringRequest(Method.POST, METHOD_ACCOUNT_LOGIN,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {


                        JSONObject veri_json;
                        try {
                            veri_json = new JSONObject(response);


                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error.getMessage());
            }
        }) {


            protected Map<String, String> getParams()
                    throws com.android.volley.AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user", generalParams);

                return params;
            };
        };

        App.getInstance().addToRequestQueue(myReq);
    }

but I want to import inside this code

params.put("user",generalParams)

these lines:

generalParams.put("name",notificationName);
generalParams.put("notificationId", notificationID);
generalParams.put("phoneNumber", phoneNumber);
generalParams.put("regionType", regionType);
generalParams.put("countryCode", countryCode);
generalParams.put("platform", platform);

here is what I want to send:

"user":{
    "name":"John",
    "notificationId":"id",
    "phoneNumber":"number",
    "regionType":"en",
    "countryCode": "+10",
   "platform":”ios”
}

However, I don't know how to handle this.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
canberk cakmak
  • 145
  • 1
  • 1
  • 16

1 Answers1

2

Firstly, a 406 means you are sending the data incorrectly, or the server does not accept the data sent to it at that address. If you are confident the server does accept JSON data, then moving on...

In order to create a JSONObject like this { user: {} }, you need to put a JSONObject at the "user" key, not an explicit HashMap because it'll toString it.

Therefore, instead, do like so

protected Map<String, String> getParams() {
    // Outer object
    Map<String, String> params = new HashMap<String, String>();

    // Inner object
    Map<String, String> userParams = new HashMap<String, String>();
    userParams.put("name",notificationName);
    userParams.put("notificationId", notificationID);
    userParams.put("phoneNumber", phoneNumber);
    userParams.put("regionType", regionType);
    userParams.put("countryCode", countryCode);
    userParams.put("platform", platform);

    JSONObject userJSON = new JSONObject(userParams);

    // Nest the object at "user"
    params.put("user", userJSON.toString());

    return params;
}

Aside:

Personally, I would recommend you change the server to accept a User in this form because that key value seems irrelevant unless the server does string type-checking on that key, which means you should ideally re-structure the API.

{ 
    "name":"John", 
    "notificationId":"id", 
    etc...
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245