I am making an android app that needs to send Post requests to my server and could really use some help! I've been trying to make these requests using android Volley. I've read a lot of related questions here and all kind of documenttion sources about android Volley but as I manage to make my requests, it's been impossible for me to receive the posted data the right way on server side and I have no clue about what I'm doing wrong... I've tried with stringRequest overriding getParams() and custom JsonObjectRequest with my data as a parameter. I get diferent results on server side with both requests but I'm still not able to catch it the right way.
On my server side my debug PHP file goes like this:
$content = file_get_contents("php://input");
$string = 'fileGetContents: ';
$json = json_decode($content, TRUE);
$string = 'fileGetContents: ';
$string .= $content;
$string .= ' -- json encoded: ';
$string .= json_encode($json);
$string .= ' -- Request Method: ';
$string .= $_SERVER['REQUEST_METHOD'];
$string .= ' -- $_REQUEST: ';
$string .= json_encode($_REQUEST);
$string .= ' -- $_POST: ';
$string .= json_encode($_POST);
$string .= ' -- user: ';
$string .= json_encode($_POST["user"]);
$string .= ' -- $_GET: ';
$string .= json_encode($_GET);
$string .= ' -- $_FORM: ';
$string .= json_encode($_FORM);
$array["ans"] = $string;
echo json_encode($array);
and as the response I get is always empty I've also tried sending my requests to http://httpbin.org/post which gives me interesting information.
So with the first stringRequest that I make like this:
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, "myServer.com/postRequest.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
displayTheResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user", "test");
params.put("pass", "passtest");
return params;
}
};
requestQueue.add(stringRequest);
I get this answer from my server:
{"ans":"fileGetContents: -- json encoded: null -- Request Method: GET -- $_REQUEST: [] -- $_POST: [] -- user: null -- $_GET: []"}
And from httpbin.org i get:
{ "args": {},
"data": "",
"files": {},
"form": { "pass": "passtest", "user": "test" },
"headers": { "Accept-Encoding": "gzip", "Content-Length": "24", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "httpbin.org", "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86 Build/NPF26K)" },
"json": null,
"origin": "104.221.22.144",
"url": "http://httpbin.org/post"
}
I can see that the information is actually sent but not received as POST data but as Form data which I don't really know what it is or how to receive it from PHP...
Then in my second JsonObjectRequest that goes like this:
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("user", "test");
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
"myServer.com/postTest.php",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
displayResponse(response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LogInActivity.this, "Error "+error.getMessage(), Toast.LENGTH_LONG).show();
fm.popBackStack();
}
});
requestQueue.add(myRequest);
From httpbin.org I get:
{ "args": {},
"data": "{\"user\":\"test\"}",
"files": {},
"form": {},
"headers": { "Accept-Encoding": "gzip", "Content-Length": "15", "Content-Type": "application\/json; charset=utf-8", "Host": "httpbin.org", "User-Agent": "Dalvik\/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86 Build\/NPF26K)" },
"json": { "user": "test" },
"origin": "104.221.22.144",
"url": "http:\/\/httpbin.org\/post"
}
and here we see that now the data is shown in "data" and "json" instead of in "form" but from my server I still get the same empty response so I gess my problem comes from the server side PHP code I'm using. I also note that "$_SERVER['REQUEST_METHOD']" is returning "GET".
I've tried a lot of diferent ways to make my Volley request but I don't know what else to do. I've tried creating a helper class, custom JsonObjectRequest, overriding getHeaders(), even adding the request to the queue with the AppController... I guess I don't have enough understanding onhow the Post requests work, I'm pretty new with android develloping.
I would really be so grateful for any help to understand how am I supposed to receive my data on server side with the Volley POST method. Thank you very much for your time!