Reputation: 1618
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
Upvotes: 0
Views: 1113
Reputation: 6797
How to post JSON to PHP with curl
use file_get_contents('php://input');
instead $_POST['json']
Upvotes: 2
Reputation: 9318
You creating JsonObject not JsonArray, so try to:
echo $data->email;
Upvotes: 1