0

我正在尝试使用 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);
?> 

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

4

1 回答 1

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);
?> 
于 2013-07-12T05:50:49.653 回答