1

Hi every body i have sending a JSON object to php server .my json is as below-

        {"cart_detail":{"product_id":"66922","ref_id":"chand09","user_id":"1"}}

I am sending JSON by below code (based on this answer by Sachin Gurnani)-

      public void postData(String result,JSONObject obj) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    System.out.println(" before JSON string");
    String json=obj.toString();

    try {

        HttpPost httppost = new HttpPost(result.toString());
        httppost.setHeader("Content-type", "application/json");
        System.out.println("After httpost header");
        StringEntity se = new StringEntity(obj.toString());   
        System.out.println(" sending json string"+se);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 
   
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
       
        String temp = EntityUtils.toString(response.getEntity());
       
          System.out.println(" i am getting response entity "+entity);
         

        Log.i("tag", temp);
        System.out.println(" item send sucessfully to cart by server");

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }
}

My PHP to handle the response is as-

      $data = array("cart_details" => array($_REQUEST));
       echo "<pre>";
       print_r($data);
       echo "</pre>";

In response i am getting stack-Trace is as follow-

    07-26 14:57:45.124: I/System.out(7191):  i am getting response entity org.apache.http.conn.BasicManagedEntity@405d2fd0
    07-26 14:57:45.124: I/System.out(7191):  i am getting response string org.apache.http.impl.client.ClientParamsStack@405134a8
   07-26 14:57:45.124: I/tag(7191): <pre>Array
   07-26 14:57:45.124: I/tag(7191): (
   07-26 14:57:45.124: I/tag(7191):     [cart_details] => Array
   07-26 14:57:45.124: I/tag(7191):         (
  07-26 14:57:45.124: I/tag(7191):             [0] => Array
  07-26 14:57:45.124: I/tag(7191):                 (
  07-26 14:57:45.124: I/tag(7191):                 )
   07-26 14:57:45.124: I/tag(7191):         )
   07-26 14:57:45.124: I/tag(7191): )
   07-26 14:57:45.124: I/tag(7191): </pre>
  07-26 14:57:45.124: I/System.out(7191):  item send sucessfully to cart by server

I did not know what problem is going on. I am new for json. How i will check that my request is going or not. In response i am receiving same json , but why this json is empty? Is it fault of android part or PHP Server .Thanks in advance to all.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • Try var_dump($_REQUEST) if that is empty then you are either not sending your data to the server or something is unsetting it – Anigel Jul 26 '13 at 09:44
  • Instead of jSon use Http Post request to send a request to PHP server, for sending huge chunks of data it would be useful. – onkar Jul 26 '13 at 09:44
  • @onkar....Yes i am using ..HttpPost httppost = new HttpPost(result.toString()); – DJhon Jul 26 '13 at 09:48
  • Try debugging both your server and app: check that you are sending valid JSON-string and that you receiving it on server and it's valid too. – Semyon Danilov Jul 26 '13 at 09:48
  • Use This Stack Example http://stackoverflow.com/questions/17057712/pass-arraylist-bean-from-android-to-webservice-php/17058208#17058208 – Karan Mavadhiya Jul 26 '13 at 09:49
  • @KaranMavadhiya.. Thanks for your response. But your link is not useful for me. – DJhon Jul 26 '13 at 09:56

1 Answers1

2

You are sending a JSON encoded POST body, thus you have to read from the input stream:

$data = array("cart_details" => json_decode(file_get_contents('php://input')),
echo "<pre>";       
print_r($data);
echo "</pre>";

the $_REQUEST and $_POST arrays will only be populated with urlencoded form data.

EJTH
  • 2,178
  • 1
  • 20
  • 25