'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.
Solution 1:[1]
StringRequest postRequest = new StringRequest(Request.Method.POST, CHAT_URL_FEED,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("THISSSSSSSS", response.toString());
if (response != null) {
parseChatJsonFeed(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// 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;
}
};
Volley Doesn't Support Parameters when its requesting for JSONObject. you can get response as JSON String and can convert back to JSON using JSONObject class.
Solution 2:[2]
For sent POST request you can write custom request class that based on JsonRequest class P.S. I use this way in my library (based on Volley)
Solution 3:[3]
You can also send a String request (JSON) as a POST as seen below.
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String requestStr = "{\n" +
"\"id\": \"your JSON\"\n" +
"}\n";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,"http://<Your url>",
requestStr,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Your Logic on Success
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Your Logic on Error
}
});
queue.add(request);
Solution 4:[4]
In spite of sending the request as a StringRequest, use JsonObjectRequest. I'm pretty sure the issue will be gone!
Thank me later :)
Solution 5:[5]
I'm a bit late to answer this, but I think it may help someone facing same problem. I search through all similar questions on SO, relating to PHP and Volley api but I didn't find any satisfying answer.
The problem is that you are sending data from the Volley library as content type
application/json
but your PHP script is expecting POST data as content type
application/x-www-form-urlencoded
In your PHP script, do this:
$_POST = json_decode(file_get_contents('php://input'), true);
if ( !empty($_POST['id']) ) {
$id = $_POST['id'];
echo $id;
}
Now if you check for
if( isset($_POST['id']) ){
echo "something";
}
it should work now
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahmed meeran |
| Solution 2 | a.black13 |
| Solution 3 | Ruchira Randana |
| Solution 4 | Vivek Bhardwaj |
| Solution 5 | Oposinii |
