【android学习笔记】与MYSQL交互之JSONARRAY

来源:互联网 发布:org.apache.http.util 编辑:程序博客网 时间:2024/05/29 11:56

         要实现的功能就是遍历数据库中的某个表格,将表格中的所有信息包裹成JSONARRAY传递给android应用。



   PHP中的代码如下:

class my_sql{     //自定义的sql类,封装常用的连接数据库方法。用的时候方便一些
private $hostName;
private $userName;
private $pwd;
private $dbName;
private $tableName;
function __construct($hostName,$dbName,$tableName,$userName,$pwd){
$this->dbName=$dbName;
$this->hostName=$hostName;
$this->pwd=$pwd;
$this->tableName=$tableName;
$this->userName=$userName;
}
function connect(){
mysql_connect($this->hostName,$this->userName,$this->pwd) or die("服务器连接成功");
}
function query($v){
return mysql_query($v);
}
function insert($tableName,$name,$value){
$this->query("INSERT INTO $tableName($name) VALUES($value)");
}
function select($tableName,$name,$value){
$this->query("SELECT * FROM $tableName WHERE $name=$value");
}
}

     mysql_query('SET NAMES utf8',$connect);   
     $mysql=new my_sql('localhost','test','one','root','');   //new出my_sql对象,连接数据库
 
   $array = array();                                                                  // 定义数组,用来装载遍历数据   
   $result=mysql_query("SELECT*FROM one");          //查询整个one表
   while($row=mysql_fetch_row($result)){                  
   $array[] = $row;                                                   //将每一行装到【数组】中再放入array中。
   }
   echo json_encode($array);                                    //用echo发送到android项目中



android代码如下:


                ArrayList<Map<String,Obejct>> jslists=new ArrayList<Map<String,Object>>();  //用来接受存储信息的容器

                System.out.println("开始接收");
str=new StringBuilder();
new AsyncTask<Void, Void, Void>() {                                              //接受信息最好新开线程
protected Void doInBackground(Void... params) {
try {
DefaultHttpClient dhc = new DefaultHttpClient();// 创建客户端对象
HttpPost hp = new HttpPost(url_up); // 创建客户端请求对象
// =======================================================================
HttpResponse httpResponse;
httpResponse = dhc.execute(hp);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
System.out.println("Response content length: "
+ httpEntity.getContentLength());
}
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(
httpEntity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line);            //将echo传来的数据拼装到str中
}
String newstr=str.toString();  
System.out.println("str"+newstr);
JSONArray ja=new JSONArray(newstr); //str转换成字符串,用JSONArray识别出来
System.out.println(ja.length());
for (int j = 0; j < ja.length(); j++) {    //按次序取出要用的部分,装载在容器中
JSONArray aa=ja.getJSONArray(j);
System.out.println(aa.get(1));
Map<String,Object> map=new HashMap<String, Object>();
map.put("id", aa.get(0));           
map.put("name", aa.get(1));
jslists.add(map);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {            
Toast.makeText(getApplicationContext(), "接受完毕", 1).show();
super.onPostExecute(result);
}
}.execute();

0 0
原创粉丝点击