我想从我的sql发送字符串数组到android并在列表视图中显示它们,我是新的android并且不知道如何
提前致谢
继承我的PHP代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
date_default_timezone_set('Asia/Singapore');
$date=date('Y-m-d');
$time=date('H:i:s');
$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()){
foreach($row as $key=>$val){
echo $val."<br>";
}
}
}
$conn->close();
?>
我回应只是为了表明我的代码是否正在检索
答案 0 :(得分:0)
在android方面,你可能想要创建一个反映来自网站的记录的类。类似的东西:
public class myWebStuff {
private static JSON_ID = "id";
private static JSON_TITLE = "title";
private int mId;
private String mTitle;
public myWebStuff() {
mId = 0;
mTitle = null;
}
public JSONObject toJSON throws JSONException {
JSONObject json = new JSONObject;
json.put(JSON_ID, mId);
json.put(JSON_TITLE, mTitle);
...etc.
return json;
}
如果您想保存从网站上提取的数据,您还会发现将JSON格式的字符串写入Android设备上的本地文件非常方便。
我希望这会有所帮助。