如何在屏幕上按ID或名称获取和显示JSON数据(Android)

问题描述 投票:0回答:3

考虑:

      public RetroPhoto(Integer id, String email, String first_name, String last_name, String avatar) {
        this.id = id;
        this.email = email;
        this.first_name = first_name;
        this.last_name = last_name;
        this.avatar = avatar;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
}

假设我在屏幕上有一个编辑文本和按钮。我必须通过JSON数据按ID提取人员的详细信息。

因此,当我将id键入为“ 1”时,它将在按钮下方以文本视图显示JSON数据中该特定人员的详细信息。这样做的逻辑是什么?

上面是JSON。

android json android-edittext android-button
3个回答
0
投票

如果要获取完整的JSON内容,则需要解析列表并获取与人员ID相关的JSON对象。

在onClick中,您可以通过以下方式获取ID:>

// Probably check if it's a valid number & non empty
int id = parseInt(editTextId.getText().toString())

retrofitInstance.apiCall.enqueue ...... 
....
onResponse (){
  List<RetroPhoto> list = response.body();
  boolean idFound = false;
  for(int i=0; i< list.size(); i++){
     if(list.get(i).getId() == id){
        // you can get details of person here
        idFound = true;
     }
  }
  if(idFound == false) {
     // Toast invalid id message
  }
}
...

因为您正在使用翻新。


0
投票

首先,您需要使用网络库,例如Retrofit,Volley等,以通过API从服务器获取json。相比Volley而言,翻新要快得多,使用post方法将id发布到服务器,并且需要从api端获取基于该id的数据并将其转换为JSON并发送回响应。通过此链接了解更多详细信息。 https://square.github.io/retrofit


0
投票

当您获得要显示的值时,可以在此处使用此

© www.soinside.com 2019 - 2024. All rights reserved.