管理MySQL数据库最简单和最便利的方式是PHP脚本。
运行PHP脚本使用HTTP协议和android系统连接。
我们以JSON格式编码数据,因为Android和PHP都有现成的处理JSON函数。

下面示例代码,根据给定的条件从数据库读取数据,转换为JSON数据。
通过HTTP协议传给android,android解析JSON数据。

定义在MySQL有以下表,并有一些数据

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)
我们想要获得在一个指定年出生的人的数据。
PHP代码将是非常简单的:连接到数据库——运行一个SQL查询,根据设定WHERE语句块得到数据——转换以JSON格式输出    
例如我们会有这种功能getAllPeopleBornAfter.php文件:

<?php/* 连接到数据库 */mysql_connect("host","username","password");mysql_select_db("PeopleData");/* $_REQUEST['year']获得Android发送的年值,拼接一个SQL查询语句 */$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'"); while($e=mysql_fetch_assoc($q))        $output[]=$e;/* 转换以JSON格式输出  */print(json_encode($output));mysql_close();?>

Android部分只是稍微复杂一点:用HttpPost发送年值,获取数据——转换响应字符串——解析JSON数据。
最后使用它。
String result = "";/* 设定发送的年值 */ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("year","1980"));/* 用HttpPost发送年值 */try{        HttpClient httpclient = new DefaultHttpClient();        HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");//上面php所在URL        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        HttpResponse response = httpclient.execute(httppost);        HttpEntity entity = response.getEntity();        InputStream is = entity.getContent();}catch(Exception e){        Log.e("log_tag", "联网错误 "+e.toString());}/* 转换响应字符串 */try{        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);//注意"iso-8859-1"编码不支持中文。        /* 如需支持中文,设定MySQL为UTF8格式,在此也设置UTF8读取 */        StringBuilder sb = new StringBuilder();        String line = null;        while ((line = reader.readLine()) != null) {                sb.append(line + "\n");        }        is.close();        result=sb.toString();}catch(Exception e){        Log.e("log_tag", "转换响应字符串错误 "+e.toString());}/* 解析JSON数据 */try{        JSONArray jArray = new JSONArray(result);        for(int i=0;i<jArray.length();i++){                JSONObject json_data = jArray.getJSONObject(i);                Log.i("log_tag","id: "+json_data.getInt("id")+                        ", name: "+json_data.getString("name")+                        ", sex: "+json_data.getInt("sex")+                        ", birthyear: "+json_data.getInt("birthyear")                );        }}}catch(JSONException e){        Log.e("log_tag", "解析JSON数据错误 "+e.toString());}

注意:android4.0以上联网代码只能放在子线程

更多相关文章

  1. 从头学Android之Android的数据存储--SQLite
  2. 安卓混合开发——Android原生和H5数据交互,详细Demo
  3. 【转载】Android数据库事务浅析
  4. Android创建和使用数据库详…
  5. 【Android 开发】:数据存储之 SQLite 数据库操作(一)
  6. Android数据库编程教程
  7. android wav 去头信息转化成pcm 格式数据

随机推荐

  1. 如何使用 Bundle 传递数据?
  2. android 基于基站,apn,gps,wifi,network
  3. Android(安卓)View框架总结(三)View工作原
  4. ------------------Android中对GridView,
  5. appium自动化测试总结(转载liangww)
  6. android 软件开发错误-解决记录
  7. [经验总结]eclipse转向android studio常
  8. Android(安卓)Studio开发工具异常与解决
  9. Flutter 启动页
  10. Android(安卓)zip文件压缩解压