Android远程连接 MYSQL
Android虽然自带java.sqlphp远程连接mysql数据库包,但是各数据库的JDBC Driver是否可用争议很多,不论国内网站还是国外网站,有人说能用,有人说不行,有人说虚拟机上能跑,上真手机就不行,有人说自己在手机上测试过也能跑。
但不管怎么说,直接连接远程数据库被公认不是一个很好的做法,至少在安全性上非常差的,所以现在最简单也是最流行的做法是访问远程服务器前段的PHP,PHP函数完成数据库操作,把结果经过JSON编码后传回,Android端再parse出结果。
SQL数据库创建测试表people 代码如下:
CREATE TABLE `people` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`sex` BOOL NOT NULL DEFAULT '1',
`birthyear` INT NOT NULL
)
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`sex` BOOL NOT NULL DEFAULT '1',
`birthyear` INT NOT NULL
)
PHP前端文件getAllPeopleBornAfter.php代码如下,查询出生年月在year之后的人:
<?php
mysql_connect("host","username","password");
mysql_select_db("PeopleData");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
mysql_connect("host","username","password");
mysql_select_db("PeopleData");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
Android客户端的方法略微复杂一点,代码如下:
String result = "";
//首先使用NameValuePair封装将要查询的年数和关键字绑定
//首先使用NameValuePair封装将要查询的年数和关键字绑定
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//使用HttpPost封装整个SQL语句
//使用HttpClient发送HttpPost对象
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("localhost/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = ute(httppost);
HttpEntity entity = Entity();
InputStream is = Content();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
/
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//使用HttpPost封装整个SQL语句
//使用HttpClient发送HttpPost对象
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("localhost/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = ute(httppost);
HttpEntity entity = Entity();
InputStream is = Content();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
/
/将HttpEntity转化为String
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = adLine()) != null) {
sb.append(line + "\n");
}
is.close();
String();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = adLine()) != null) {
sb.append(line + "\n");
}
is.close();
String();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//将String通过JSONArray解析成最终结果
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = JSONObject(i);
Log.i("log_tag","id: "+Int("id")+
", name: "+String("name")+
", sex: "+Int("sex")+
", birthyear: "+Int("birthyear")
);
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论