![](/img/trans.png)
[英]How to decode json in php coming from android json post volley
[英]How to decode JSON in PHP values passed from Android
我在做Android应用。 在那我将值传递给PHP文件。 在传递到PHP文件之前,我使用日志查看了当时的值,但我获得了所有值,但是在插入数据时未提交。 我做了很多尝试,但无法识别出哪里出了问题。
以下是在日志目录中的日志:
[{"MNO":"5656565664","Latitude":"17.3731701","Longitude":"78.504573"}]
以下是我使用Log.e的doInBackground方法
@Override
protected Boolean doInBackground(String... arg0)
{
Log.e("doInBackground", "doInBackground1");
Log.e("arg0[0]", arg0[0]);
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Location", arg0[0]));
Log.e("doInBackground", "doInBackground2");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/insertLocation.php");
HttpParams httpParameters = new BasicHttpParams();
httpclient = new DefaultHttpClient(httpParameters);
Log.e("doInBackground", "doInBackground3");
try {
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response;
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
Log.e("Google", "Server Responded OK");
} else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
以下是我要插入的insertLocation.php代码:
<?php
$Location= $_POST['Location'];
$data = json_decode($Location, true);
$mno = $data ->MNO;
$latitude = $data ->Latitude;
$longitude =$data ->Longitude;
//Include db connect class
$hostname_localhost ="localhost";
$database_localhost ="geolocation";
$username_localhost ="abc";
$password_localhost ="abc123";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$flag['code']=0;
//Mysql inserting a new
$result = mysql_query("INSERT INTO location(mno,latitude,longitude) VALUES('$mno','$latitude','$longitude')");
if($result)
{
$flag['code']=200;
}
echo json_encode($flag);
mysql_close($con);
?>
$data = json_decode($Location, true);
这将返回数组
所以
使用$data[0]['MNO']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.