title: Send post request using Volley and receive in PHP
body: <p>I am trying to use volley in my project to handle all my HTTP request since it's the most efficient one as far as I know. So I started to learn volley by following this <a href="http://www.androidhive.info/2014/05/android-working-with-volley-library-1/">AndroidHive tutorial</a>.</p>
  
  <p>My first GET request was successful. Then I moved on to POST request and I failed. I saw on Stack Overflow many people had problems combining post request of volley with PHP. I believe we cannot access it using the normal way that is <code>$_POST[""]</code> as volley sends a JSON object to the URL which we specify.</p>
  
  <p>There were lots of solutions which I tried but didn't succeed. I guess there should be a simple and standard way of using volley with PHP. So I would like to know what do I need to do in order to receive the json object sent by volley in my PHP code. </p>
  
  <p>And also how do I check if volley is really sending a JSON object?</p>
  
  <p><strong>My volley code to send simple post request:</strong></p>
  
  <pre><code>JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                  url, null,
                  new Response.Listener&lt;JSONObject&gt;() {
  
                      @Override
                      public void onResponse(JSONObject response) {
                          Log.d(TAG, response.toString());
                          pDialog.hide();
                      }
                  }, new Response.ErrorListener() {
  
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          VolleyLog.d(TAG, "Error: " + error.getMessage());
                          pDialog.hide();
                      }
                  }) {
  
              @Override
              protected Map&lt;String, String&gt; getParams() {
                  Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
                  params.put("name", "Droider");
                  return params;
              }
  
          };
  
  // Adding request to request queue
  AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
  </code></pre>
  
  <p><strong>My PHP code to receive json object:</strong> (I am pretty sure this is the wrong way, I am not that good in PHP)</p>
  
  <pre><code>&lt;?php
      $jsonReceiveData = json_encode($_POST);
      echo $jsonReceivedData;
  ?&gt;
  </code></pre>
  
  <p>I tried lots of ways of accepting JSON object in PHP like this one as well 
  <code>echo file_get_contents('php://input');</code> </p>
  
  <p><strong>The Result</strong> </p>
  
  <pre><code>null
  </code></pre>
  
  <p><strong>EDIT (The correct way thanks to Georgian Benetatos)</strong></p>
  
  <p>I created the class as you mentioned the class name is <code>CustomRequest</code> which is as follows:</p>
  
  <pre><code>import java.io.UnsupportedEncodingException;
  import java.util.Map;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import com.android.volley.NetworkResponse;
  import com.android.volley.ParseError;
  import com.android.volley.Request;
  import com.android.volley.Response;
  import com.android.volley.Response.ErrorListener;
  import com.android.volley.Response.Listener;
  import com.android.volley.toolbox.HttpHeaderParser;
  
  public class CustomRequest extends Request&lt;JSONObject&gt;{
  
        private Listener&lt;JSONObject&gt; listener;
        private Map&lt;String, String&gt; params;
  
        public CustomRequest(String url, Map&lt;String, String&gt; params,
                  Listener&lt;JSONObject&gt; reponseListener, ErrorListener errorListener) {
              super(Method.GET, url, errorListener);
              this.listener = reponseListener;
              this.params = params;
        }
  
        public CustomRequest(int method, String url, Map&lt;String, String&gt; params,
                  Listener&lt;JSONObject&gt; reponseListener, ErrorListener errorListener) {
              super(method, url, errorListener);
              this.listener = reponseListener;
              this.params = params;
          }
  
      @Override
      protected Map&lt;String, String&gt; getParams() throws com.android.volley.AuthFailureError {
        return params;
      };
  
      @Override
      protected void deliverResponse(JSONObject response) {
          listener.onResponse(response);
      }
  
      @Override
      protected Response&lt;JSONObject&gt; parseNetworkResponse(NetworkResponse response) {
           try {
                  String jsonString = new String(response.data,
                          HttpHeaderParser.parseCharset(response.headers));
                  return Response.success(new JSONObject(jsonString),
                          HttpHeaderParser.parseCacheHeaders(response));
              } catch (UnsupportedEncodingException e) {
                  return Response.error(new ParseError(e));
              } catch (JSONException je) {
                  return Response.error(new ParseError(je));
              }
      }
  
  }
  </code></pre>
  
  <p>Now in my activity I called the following:</p>
  
  <pre><code>String url = some valid url;
  Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
  params.put("name", "Droider");
  
  CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, new Response.Listener&lt;JSONObject&gt;() {
  
              @Override
              public void onResponse(JSONObject response) {
                  try {
                      Log.d("Response: ", response.toString());
                  } catch (JSONException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          }, new Response.ErrorListener() {
  
              @Override
              public void onErrorResponse(VolleyError response) {
                  Log.d("Response: ", response.toString());
              }
          });
          AppController.getInstance().addToRequestQueue(jsObjRequest);
  </code></pre>
  
  <p><strong>My PHP code is as follow:</strong></p>
  
  <pre><code>&lt;?php
  $name = $_POST["name"];
  
  $j = array('name' =&gt;$name);
  echo json_encode($j);
  ?&gt;
  </code></pre>
  
  <p>Now its returning the correct value:</p>
  
  <pre><code>Droider
  </code></pre>