如何显示从 php 到 android 列表视图的数组?

how to display array from php to android listview?

在我的 logcat 中,我返回了一些数组,我想在 android 列表视图中列出它们,我不知道它是如何工作的,因为我是这个 android 东西的新手

提前感谢 :)

我的php代码:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "android_sms";
date_default_timezone_set('Asia/Singapore'); 


$date=date('Y-m-d');
$time=date('H:i:s');
$returnVal = array();
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else{

//$sql = "select * from sample order by time desc limit 1";
$sql = "select * from sample";
$res = $conn->query($sql);

while($row =$res->fetch_assoc()){
    $d = array();
    foreach($row as $key=>$val){
        $d[] = $row;
        $returnVal["data"][]=$val;
    }           
}
print_r($returnVal["data"]);


}
$conn->close();

?>

我的android代码:

@Override
protected Void doInBackground(Void... arg0) {
try{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();       
    nameValuePairs.add(new BasicNameValuePair("message",message));

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost= new HttpPost("http://192.168.0.111/android/dbselect.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

    //httpclient.execute(httppost);
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    String responseStr = EntityUtils.toString(entity);
    Log.e("LOGsadasd", responseStr);

    }catch(Exception e){
    e.printStackTrace();
    }
return null;
}

在我的日志猫中我返回了这些数组

Array
(
    [0] => 5
    [1] => niko
    [2] => 2015-02-13 13:54:24
    [3] => 6
    [4] => noel
    [5] => 2015-02-13 14:23:34
    [6] => 7
    [7] => santos
    [8] => 2015-02-13 14:35:09
)

如何在列表视图中显示它们? 提前tnx

你应该写一个网络服务来读取网络上的数据。之后你的 post 或 get 方法应该 return JSON string 。您可以轻松读取应用程序中的值。这些链接会有所帮助

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php