Php 从android应用程序插入mysql
我是android开发新手。作为一个更大项目的一部分,我想将数据从android设备插入web服务器。因此,我做了一些研究,发表了一些文章,比如,在尝试开发一个测试应用程序时非常有帮助,该应用程序插入到mysql数据库中,mysql数据库驻留在远程web服务器上 这是我的android代码Php 从android应用程序插入mysql,php,android,mysql,Php,Android,Mysql,我是android开发新手。作为一个更大项目的一部分,我想将数据从android设备插入web服务器。因此,我做了一些研究,发表了一些文章,比如,在尝试开发一个测试应用程序时非常有帮助,该应用程序插入到mysql数据库中,mysql数据库驻留在远程web服务器上 这是我的android代码 ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); N
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxxxxxx.in/installment.php");
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", editTextCustomer.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("amount", editTextAmount.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
else {
Toast.makeText(PayBillActivity.this, "Internet Access, Denied!!", Toast.LENGTH_LONG).show();
}
ConnectivityManager connMgr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo NetworkInfo=connMgr.getActiveNetworkInfo();
if(networkInfo!=null&&networkInfo.isConnected()){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxxxxxxx.in/installment.php");
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“name”,editTextCustomer.getText().toString());
添加(新的BasicNameValuePair(“amount”,editTextAmount.getText().toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
Log.i(“postData”,response.getStatusLine().toString());
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
}
否则{
Toast.makeText(PayBillActivity.this,“拒绝Internet访问!!”,Toast.LENGTH\u LONG.show();
}
下面是php代码
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['name']) && isset($_POST['amount'])) {
$name = $_POST['name'];
$amount = $_POST['amount'];
$con=mysqli_connect("localhost","db_user","passwd","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO installment (name, amount) VALUES ('$_POST[name]','$_POST[amount]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
// check if row inserted or not
if ($sql) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Installment made successfully";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
echo $result;
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
//$amount = 1000;
//echo $amount;
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
我想如果你花时间把这个问题发布到谷歌上,你可能会得到一些好答案。。。只是为了完成这个问题
有两个选项可供选择,要么你可以添加一行代码,允许在主线程上进行网络操作,但这对你的应用程序和编码风格都非常不利
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(策略)
较长的选项是重新设计代码,使网络操作在单独的线程中执行。这对应用程序都有好处,您将学习如何使用多线程程序 我认为你不应该使用那种严格的方法,也不应该手动将其从main中删除
只需使用一个智能的预制库,它将为您创造一切
下载:
注意:这个lib甚至使用gzip压缩请求:)Android不允许/强烈阻止您在主线程中执行与网络相关的进程。如果在主线程中执行,与网络相关的进程可能会延迟应用程序很长时间。建议您将这些进程放在异步线程中。如果您将“NetworkOnMainThreadException”放在该站点的搜索框中,您将获得342个结果。那应该对你有帮助