To send json from android to php, I used the Volley library StringRequest .
StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() { @Override public void onResponse(String response) { // some code } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //some code } }){ @Override protected Map<String,String> getParams(){ Map<String, String> params = new HashMap<String, String>(); ArrayList<Command> commands = MyApplication.readFromPreferences(getActivity(), Constants.COMMAND); String jsonCommands = new Gson().toJson(commands); params.put("commands", jsonCommands); return params; } };
And in order to catch the data in php and check if it was sent correctly, I used this
echo $_POST["commands"];
Conclusion:
[{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"CF77 COIN FINDER\",\"url_image\":\"IMG_76ECDC-707E7E-70AC81-0A1248-4675F3-F0F783.jpg\",\"name\":\"CF77 COIN FINDER\",\"pid\":12,\"price\":500.0},\"product_quantity\":3},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"JEOSONAR 3D DUAL SYSTEM\",\"url_image\":\"IMG_2D9DF0-2EB7E9-ED26C0-2C833B-B6A5C5-5C7C02.jpg\",\"name\":\"JEOSONAR 3D DUAL SYSTEM\",\"pid\":15,\"price\":500.0},\"product_quantity\":1},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"MAKRO POINTER\",\"url_image\":\"IMG_Macro.jpg\",\"name\":\"MAKRO POINTER\",\"pid\":18,\"price\":500.0},\"product_quantity\":3}]
I noticed that when sending a json string with the POST method using the Volley library, a lot of anti-slash was added to avoid double quotes.
So here is my problem:
I want to decode json for an array of objects in php, so I used
$commands = json_decode( $_POST["commands"],true);
But it always returns an empty array due to invalide above json (caused by anti-slash).
Is there a way in php or in the java SDK that provides a contract to send and receive json without any problems? Or should I reformat json to php and remove all anti-slashes?