无法通过翻新从json中获取“名称”值

时间:2019-07-18 17:07:56

标签: android json retrofit

我在从mysql数据库获取“ ad”值时遇到问题。

这是我的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("]");
?>

当我运行代码片段时,我得到的是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ş'}]

1 个答案:

答案 0 :(得分:1)

您从服务器获取的数据是

{ "id": "27", "ad": "Aslı", "soyad": "Şafak" }

您要在改造客户端中打印的数据是

Kullanici{id='27', isim='null', soyad='Şafak'}

服务器正在发送键名 ad 中的值“ Asli”。您的客户端具有成员变量 isim ,这是一个不同的键名,这就是为什么它没有被映射的原因。

在客户端中,

将您的成员变量名称从 isim 更改为 ad

OR

如果您使用的是GSON,请在其上方放置注释。

@SerializedName("ad")
String isim 

这会将键 ad 的值映射到 isim