Volley JsonObjectRequest Post parameters no longer work
You just have to make a JSONObject from your HashMap of parameters:
String url = "https://www.youraddress.com/";Map<String, String> params = new HashMap();params.put("first_param", 1);params.put("second_param", 2);JSONObject parameters = new JSONObject(params);JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { //TODO: handle success }}, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); //TODO: handle failure }});Volley.newRequestQueue(this).add(jsonRequest);
I ended up using Volley's StringRequest instead, because I was using too much valuable time trying to make JsonObjectRequest work.
RequestQueue queue = Volley.newRequestQueue(this);String url ="http://myserveraddress";StringRequest strRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("tag", "test"); return params; } };queue.add(strRequest);
This worked for me. Its just as simple as JsonObjectRequest, but uses a String instead.
I had a similar problem, but I found out that the problem was not on the client side, but in the server side. When you send a JsonObject
, you need to get the POST object like this (in the server side):
In PHP:
$json = json_decode(file_get_contents('php://input'), true);