从数据库android加载数据到listview

时间:2013-07-12 05:14:01

标签: php android

我正在尝试使用php加载mysql / localhost数据库中的表到android。我写了jason代码如下。

 try {
            // convert json string to json array
            JSONArray aJson = new JSONArray(sJson);
            // create apps list
            List<Application> apps = new ArrayList<Application>();

            for(int i=0; i<aJson.length(); i++) {
                JSONObject json = aJson.getJSONObject(i);
                Application app = new Application();
                app.setTitle(json.getString("number"));
                app.setTotalDl(Long.parseLong(json.getString("speed")));
                //app.setRating(Integer.parseInt(json.getString("rating")));  
                //app.setIcon(json.getString("icon"));

                // add the app to apps list
                apps.add(app);
            }

            //notify the activity that fetch data has been complete
            if(listener != null) listener.onFetchComplete(apps);
        } catch (JSONException e) {
            msg = "Invalid response";
            if(listener != null) listener.onFetchFailure(msg);
            return;
        }  

在那里你可以看到我的php文件。

<?php

//connect to the db

$host="localhost"; // Host name 
$user="root"; // Mysql username 
$pswd=""; // Mysql password 
$db="gpsvts_geotrack"; // Database name 
 // Table name



$conn = mysqli_connect($host,$user,$pswd,$db);

//mysql_select_db($db, $conn);
//run the query to search for the username and password the match
//$query = "SELECT * FROM "."  ".$tbl_name. "  "."WHERE uname = '$myusername' AND passwd= '$mypassword' ";
$query = "select user_master.uid,device_locator_tbl.imei,device_locator_tbl.speed,device_locator_tbl.datetime,device_locator_tbl.number,device_master.icon 
from device_locator_tbl,device_master,device_registration,user_master where user_master.uid=device_registration.uid
 AND device_registration.imei=device_master.imei AND device_registration.imei=device_locator_tbl.imei AND user_master.uid=126";
//$query = "SELECT uid FROM $tbl_name WHERE uname = '$un' AND passwd = '$pw'";
$result = mysqli_query($conn,$query) or die("Unable to verify user because : " );
//this is where the actual verification happens

if($row = mysqli_fetch_array($result))
//echo mysql_result($result,0);  // for correct login response
{

 $rows[] = $row; 
 }
 // close the database connection
mysqli_close($con);

// echo the application data in json format
echo json_encode($rows);
?> 

这里我正确设置了路径。我想将这些表格细节添加到listview中。运行时,应用列表视图已加载。但是没有加载数据。我总是收到错误消息“无效响应”。我想纠正它。怎么做?

1 个答案:

答案 0 :(得分:1)

如果你使用mysql作为这样的数据库更新服务器代码

<?php

//connect to the db

$host="localhost"; // Host name 
$user="root"; // Mysql username 
$pswd=""; // Mysql password 
$db="gpsvts_geotrack"; // Database name 
$conn = mysql_connect($host,$user,$pswd);
mysql_select_db($db, $conn);
$query = "select user_master.uid,device_locator_tbl.imei,device_locator_tbl.speed,device_locator_tbl.datetime,device_locator_tbl.number,device_master.icon 
from device_locator_tbl,device_master,device_registration,user_master where user_master.uid=device_registration.uid
 AND device_registration.imei=device_master.imei AND device_registration.imei=device_locator_tbl.imei AND user_master.uid=126";
//$query = "SELECT uid FROM $tbl_name WHERE uname = '$un' AND passwd = '$pw'";
$result = mysql_query($conn,$query) or die("Unable to verify user because : " );


if($row = mysql_fetch_array($result))

{

 $rows[] = $row; 
 }

mysql_close($con);

// echo the application data in json format
echo json_encode($rows);
?>