0

I have used httppost to send json object from android to my php file my java code is

    JSONObject json = new JSONObject();
    try
    {
        json.put("email", "15");

    }
    catch (JSONException e)
    {

        e.printStackTrace();
    }
    String url = "http://xxxx.in/xxx/xxx.php";
    HttpResponse re;
    String temp = new String();
    try
    {
        re = HTTPPoster.doPost(url, json);
        temp = EntityUtils.toString(re.getEntity());
        Log.d("Main",temp);
    }
    catch (ClientProtocolException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (temp.compareTo("SUCCESS")==0)
    {
        Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
    }

    public class HTTPPoster
 {
public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    HttpEntity entity;
    StringEntity s = new StringEntity(c.toString());

    s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    entity = s;
    request.setEntity(entity);
    HttpResponse response;
    response = httpclient.execute(request);
    return response;
}
}

and my php code is

$data = json_decode( $_POST['json'] );
echo $data['email'];
echo "working";

only Working is ecohed back i dont get $data['email'] content

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Abhay Kumar
  • 1,582
  • 1
  • 19
  • 45

2 Answers2

2

How to post JSON to PHP with curl

use file_get_contents('php://input'); instead $_POST['json']

Community
  • 1
  • 1
Selvin
  • 6,598
  • 3
  • 37
  • 43
  • did you try `$data = json_decode( file_get_contents('php://input') );` ? if not working try both (my and jamapag's) answers `$data = json_decode( file_get_contents('php://input') ); echo $data->email;` – Selvin Jul 20 '11 at 11:18
1

You creating JsonObject not JsonArray, so try to:

echo $data->email;
jamapag
  • 9,290
  • 4
  • 34
  • 28