I'm currently trying to send a simple POST-request via Google Volley to my server. Therefore I've written the following lines of code:
Map<String, String> params = new HashMap<String, String>();
params.put("regId", "skdjasjdaljdlksajskl");
JSONObject object = new JSONObject(params);
JsonObjectRequest request = new JsonObjectRequest(Method.POST,
"address_of_my_server/method", object,
successListener, errorListener);
queue.add(request);
But I get an Error 500 returned, which says, that there is a missing parameter (regId). I've tried the same with a GET-Request, but I got the same result.
Only when I'm using a StringRequest with a formatted URL like "address_of_my_server/method?regId=sadlasjdlasdklsj" the server replies with 200.
I get the exact same result when I use a StringRequest like:
StringRequest request = new StringRequest(Method.POST,
"myurl", successListener,
errorListener){
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("regId", "skdjasjdaljdlksajskl");
return params;
}
};
Why is Volley ignoring my parameters?
Use this helper class:
Thi's my solution
Solution 1
Solution 2
remember that if you use php,the $_POST[''];
not working, her more information.
Good Luck
Woking example with the issue that Rajesh Batth mentioned
Java code:
PHP-Code:
Note:
The PHP-Vars
$_POST
and$_REQUEST
and$_GET
are empty if you are not sending additional GET-VARS.EDIT:
I deleted my previous answer since it wasn't accurate.
I'll go over what I know today: Apparently,
getParams
should work. But it doesn't always. I have debugged it myself, and it seems that it is being called when performing a PUT or POST request, and the params provided in that method are in a regular GET parameters string (?param1=value1¶m2=value2
...) and encoded and put in the body.I don't know why but for some reason this doesn't work for some servers.
The best alternate way I know to send parameters, is to put your parameters in a
JSONObject
and encode its contents in the request's body, using the request constructor.I had same issue last week, but it is fixed now. Your server accepts the Content-Type as form-data, when sending volley's JsonObjectRequest the request's content-type will be application/json so whole params will be sent as one json body, not as key value pairs as in Stringrequest. Change the server code to get request params from http request body instead of getting it from keys(like $_REQUEST['name'] in php).