Max Pain Max Pain - 21 days ago 6
Android Question

Volley not sending a post request with parameters.

I have the code below (Volley Library By Google) to send a POST request to my php server and get information as a result. I tried the code without checking

isset($_POST['id'])
in php and the code worked fine. Buy when I started to check, php will skip the if statement and go to else meaning the code is not sending the
params
correctly. How can I fix this?

RequestQueue queue = Volley.newRequestQueue(Chat.this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
CHAT_URL_FEED, null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
Log.d("THISSSSSSSS", response.toString());
if (response != null) {
parseChatJsonFeed(response);
}
}
}, new Response.ErrorListener(){

@Override
public void onErrorResponse(VolleyError error){
VolleyLog.d("Here", "Error: " + error.getMessage());
}
}) {

@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("id", id);
return params;
}

};

queue.add(jsonObjReq);


I also tried the following code:

RequestQueue queue = Volley.newRequestQueue(Chat.this);

JSONObject params = new JSONObject();
try {
params.put("id", id);
} catch (JSONException e) {
e.printStackTrace();
}


JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
CHAT_URL_FEED, params,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
Log.d("THISSSSSSSS", response.toString());
if (response != null) {
parseChatJsonFeed(response);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Here", "Error: " + error.getMessage());
}
});

queue.add(jsonObjReq);


but I still get the same result.

Answer

After spending some more time looking into this problem, I found out that Volley does not properly work with JSON request with POST requests. User @SMR suggested that in the comment section of my question. I also saw a similar answers on Google groups and the mian repo on git. I ended up using GET requests to pass the information to the server and retrieve a JSON feed.