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

0

2 Answers 2

2

How to post JSON to PHP with curl

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

Sign up to request clarification or add additional context in comments.

1 Comment

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;
1

You creating JsonObject not JsonArray, so try to:

echo $data->email;

1 Comment

Did you try var_dump($data)? Also try @Selvin's solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.