您可以参考本教程,但您确实需要一个网络服务器。
请求机制 Android App ----> webserver ------> 数据库(mysql)
响应机制 Android App <---- webserver <----- database (mysql)
Android App 将使用JSON或其他方式获取数据并显示
PHP 代码
<?php
$con=mysql_connect("host","username");
if(!$con)
{
die("Could Not Connect".mysql_error());
}
$db="CREATE DATABASE login";
mysql_query($db,$con);
mysql_select_db("login",$con);
$tab="CREATE TABLE info(FirstName varchar(20),LastName varchar(20))";
mysql_query($tab,$con);
$user_fname=$_POST['fn'];
$user_lname=$_POST['ln'];
$row= mysql_query("INSERT INTO info (FirstName,LastName) VALUES('$user_fname', '$user_lname')");
if ($row) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo $row;
}
mysql_close($con);
?>
发送数据 (Android)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1:4001/file.php");
List<NameValuePair> pair=new ArrayList<NameValuePair>(2);
pair.add(new BasicNameValuePair("fn",fname));
pair.add(new BasicNameValuePair("ln",lname));
httppost.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse response = httpclient.execute(httppost);