Php Android Json解析
我已经使用httppost将json对象从android发送到我的php文件,我的java代码是Php Android Json解析,php,android,json,parsing,Php,Android,Json,Parsing,我已经使用httppost将json对象从android发送到我的php文件,我的java代码是 JSONObject json = new JSONObject(); try { json.put("email", "15"); } catch (JSONException e) { e.printStackTrace(); } String url = "http://xxxx.in/xxx/
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;
}
}
我的php代码是
$data = json_decode( $_POST['json'] );
echo $data['email'];
echo "working";
唯一有效的是EcoHead back我没有获得$data['email']内容您创建的是JsonObject而不是JsonArray,所以请尝试:
echo $data->email;
您正在创建JsonObject而不是JsonArray,因此请尝试:
echo $data->email;
使用文件获取内容('php://input');代码>而不是$\u POST['json']
使用文件获取内容('php://input');
而不是$\u POST['json']
您是否尝试了var\u dump($data)
?还可以尝试@Selvin的解决方案。您是否尝试了var\u dump($data)
?还可以尝试@Selvin的解决方案。您是否尝试了$data=json\u解码(file\u get\u contents')php://input') );代码>?如果不工作,请尝试(my和jamapag)两种答案$data=json\u decode(file\u get\u contents('php://input') ); echo$数据->电子邮件
您是否尝试了$data=json\u解码(文件\u获取\u内容('php://input') );代码>?如果不工作,请尝试(my和jamapag)两种答案$data=json\u decode(file\u get\u contents('php://input') ); echo$数据->电子邮件代码>