Majid Hojati Majid Hojati - 9 months ago 43
Android Question

Parse request sent by Volley to PHP

I want to send a request to database using Volly in android. This is my request

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
return null;

protected void onPostExecute(Double result){
Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show();
protected void onProgressUpdate(Integer... progress){

public void postData(String valueIWantToSend) {
try {
RequestQueue requestQueue = Volley.newRequestQueue(mContexy);
String URL = Config.Get_FeedBackURL;
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", SharedPrefs.getString(mContexy, SharedPrefs.KEY_email," "));
jsonBody.put("ID", "");
jsonBody.put("Author", SharedPrefs.getString(mContexy, SharedPrefs.KEY_name," "));
jsonBody.put("comment", valueIWantToSend);
final String mRequestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
public void onResponse(String response) {
Log.i("VOLLEY", response);
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}) {
public String getBodyContentType() {
return "application/json; charset=utf-8";

public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {"Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;

protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
responseString = String.valueOf(;

// can get more details such as response.headers
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));

} catch (JSONException e) {


It sends request into php webservice but I can not handle it,I used this code in my php file

$json = file_get_contents('php://input');
$objd = json_decode($_POST);

echo $objd["ID"];
//"ID", ""
if ($objd["ID"]== "") {

echo $obj["email"];
echo $obj["Author"];
echo $obj["comment"];

//fetch product id from ibecon
$stmt = $this->db->prepare("INSERT INTO `userComments`(`useremail`, `username`, `text`) VALUES ('as','dd','dddd')");


sendResponse(200, json_encode($obj));

return true;

}else if(isset($_GET["companyid"])){

sendResponse(203, json_encode($result));

return false;



Php return
PHP Warning: json_decode() expects parameter 1 to be string, array given in
and then return status code of 200.I want to know How can I handle posted data in php file.

Answer Source

This is because json_decode accept a string as JSON , you're trying to convert all superglobal var wich is an array containing more information than just the data you're sending. Try to do instead : $objd = json_decode($json,true);

Notices the true arg meaning to cast array instead of stdObject. Also you can var_dump($POST) and you will see how is the structure of it.