【发布时间】:2019-07-19 02:50:12
【问题描述】:
我在从 mysql 数据库中获取“广告”值时遇到问题。
这是我的 listele.php 代码,用于获取包含值的 json 格式。
<?php
//If the values are not blank
//Connecting to our database by calling dbConnect script
include('connection.php');
Class Kullanici{
public $id = "";
public $ad = "";
public $soyad = "";
}
$kl = new Kullanici();
$sql = "SELECT * FROM kullanici";
$list = mysqli_query($conn,$sql);
$resultFromSql = mysqli_num_rows($list);
$sayac = 0;
echo("[");
while($result = mysqli_fetch_assoc($list)){
$sayac = $sayac + 1;
$kl->id = $result["id"];
$kl->ad = $result["ad"];
$kl->soyad = $result["soyad"];
echo json_encode($kl,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
if($resultFromSql != $sayac){
echo(",");
}
}
echo("]");
?>
当我运行代码 sn-p 时,我得到了 json 格式
[{ "id": "27", "ad": "Aslı", "soyad": "Şafak" },{ "id": "29", "ad": "Ali", "soyad": "Ak" },{ "id": "30", "ad": "Ersin", "soyad": "Demir" },{ "id": "31", "ad": "Sercan", "soyad": "Demirbaş" }]
当我从 json 格式获取这些值以将它们全部插入到列表视图中时,“ad”值为 null,但其他值有它们的值。
kullanicilist = new ArrayList<>();
Call<List<Kullanici>> x = Manager.getInstance().goster();
x.enqueue(new Callback<List<Kullanici>>() {
@Override
public void onResponse(Call<List<Kullanici>> call, Response<List<Kullanici>> response) {
Log.i(LOG,"istek | onResponse is working");
if (response.isSuccessful()) {
kullanicilist = response.body();
Log.i(LOG,"istek | kullanicilist : " + kullanicilist.toString());
adp = new KullaniciAdapter(kullanicilist, getApplicationContext(), MainActivity.this);
listView.setAdapter(adp);
}
}
@Override
public void onFailure(Call<List<Kullanici>> call, Throwable t) {
Log.i(LOG,"istek | onFailure is working");
Log.i(LOG,t.getMessage());
}
});
onResponse 中的第一个 Log 语句
I/com.example.dbtojson.activity.MainActivity: istek | kullanicilist : [Kullanici{id='27', isim='null', soyad='Şafak'}, Kullanici{id='29', isim='null', soyad='Ak'}, Kullanici{id='30', isim='null', soyad='Demir'}, Kullanici{id='31', isim='null', soyad='Demirbaş'}]
【问题讨论】:
-
您能展示一下您在 android 项目中使用的 Kullanici 类吗?