Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用HttpUrlConnection通过JSON从Android向PHP web服务发送数据_Php_Android_Json_Web Services_Urlconnection - Fatal编程技术网

使用HttpUrlConnection通过JSON从Android向PHP web服务发送数据

使用HttpUrlConnection通过JSON从Android向PHP web服务发送数据,php,android,json,web-services,urlconnection,Php,Android,Json,Web Services,Urlconnection,我目前正在为一个小项目开发一个Android应用程序,该项目包括在Android平台上学习开发。我已经完成了所有的应用程序,现在我需要用PHP和MySQL数据库将数据从应用程序发送到服务器。我有一个web服务,可以根据通过json格式发送的数据在数据库中进行插入。我已经用高级Rest客户端测试了web服务,并成功地将数据插入到数据库中。现在我正在用我的android应用程序进行测试,但它不起作用。我已经检查了连接状态,它是“OK”,但是当我检查数据库时,没有插入 以下是我的PHP web服务:

我目前正在为一个小项目开发一个Android应用程序,该项目包括在Android平台上学习开发。我已经完成了所有的应用程序,现在我需要用PHP和MySQL数据库将数据从应用程序发送到服务器。我有一个web服务,可以根据通过json格式发送的数据在数据库中进行插入。我已经用高级Rest客户端测试了web服务,并成功地将数据插入到数据库中。现在我正在用我的android应用程序进行测试,但它不起作用。我已经检查了连接状态,它是“OK”,但是当我检查数据库时,没有插入

以下是我的PHP web服务:

<?php  

$json = file_get_contents('php://input');
$obj = json_decode($json);
$con = mysql_connect('host','login','password') 
   or die('Cannot connect to the DB');
mysql_select_db('database_name',$con);
mysql_query("INSERT INTO `commercial` (firstName, surNameC, email, number, salonName, uuid)
VALUES ('".$obj->{'firstname'}."','".$obj->{'name'}."','".$obj->  {'email'}."','".$obj->{'phonenumber'}."','".$obj->{'fairreference'}."','".$obj-> {'commercialuuid'}."')");
mysql_close($con);

$posts = "INSERTION OK";
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts)); 

?>
由于我搜索了几个经过验证并基于它的线程,我真的不明白为什么它不起作用。 如果有人能帮助我,我将不胜感激

谢谢。

-->更新: 正如评论所说,这个答案现在已经被弃用了,你唯一的选择就是使用lib改型,我现在使用它没有问题。寻找它,我建议使用改型2而不是旧的。| |

您可以使用$post发送要插入的数据

将其插入到线程或异步任务中

List<NameValuePair> querypair=new ArrayList<NameValuePair>(1);
                        DefaultHttpClient httpClient = new DefaultHttpClient();;
                        querypair.add(new BasicNameValuePair("id", id));
                        querypair.add(new BasicNameValuePair("name", name));
                        querypair.add(new BasicNameValuePair("email", email));
                        HttpPost httpGetquery =new HttpPost("http://www.youradddres/insert.php");
                        httpGetquery.setEntity(new UrlEncodedFormEntity(querypair));
                        HttpResponse httpResponseGetQuery = httpClient.execute(httpGetquery);
                        httpEntityQueryGet = httpResponseGetQuery.getEntity();
                        Log.i("Entity",httpEntityQueryGet.toString());
List querypair=newarraylist(1);
DefaultHttpClient httpClient=新的DefaultHttpClient();;
add(新的BasicNameValuePair(“id”,id));
add(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“email”,email));
HttpPost httpGetquery=新建HttpPost(“http://www.youradddres/insert.php");
setEntity(新的UrlEncodedFormEntity(querypair));
HttpResponse httpResponseGetQuery=httpClient.execute(httpGetquery);
httpEntityQueryGet=httpResponseGetQuery.getEntity();
Log.i(“Entity”,httpEntityQueryGet.toString());
也上传这个php

<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";

$con=mysql_connect("localhost","$username","$password");
mysql_select_db("$dbname", $con);

$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];

mysql_query("INSERT INTO table(id, name, email)
VALUES ('$id', '$name', '$email')");

echo "New record created successfully";

?>

作为第一步,捕获mysql\u query()的结果,看看这个:谢谢你的建议。是我做的,很有用。现在我有了一些改进,我插入了,但是插入的对象中没有值。我已经打印了我在web服务中收到的json,它是空的。因此,问题在于我将json发送到web服务的方式。使用outputstreamwriter还有其他方法吗?因为我在Logcat上检查了json,它的创建和格式都很好。问题解决了!这完全是从我使用的免费服务器。。。买东西。。。我已经在本地wamp服务器上测试过了,我可以工作了!此外,您的DBuser必须具有执行插入操作的权限
DefaultHttpClient
BasicNameValuePair
在api 22中被弃用,在api 23中被删除。确实如此。现在JSON的趋势是在平台之间操作数据
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";

$con=mysql_connect("localhost","$username","$password");
mysql_select_db("$dbname", $con);

$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];

mysql_query("INSERT INTO table(id, name, email)
VALUES ('$id', '$name', '$email')");

echo "New record created successfully";

?>