简体   繁体   中英

How to fetch and display JSON data by id or name on screen (Android)

Consider:

      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;
    }
}

Suppose I have an edittext and button on the screen. I have to fetch the details of a person by ID through JSON data.

So when I type the id as "1", it should display the details of that particular person from JSON data below the button as textview. What will be the logic for it?

The above is JSON.

When you got the value which you want to display, then there you can use this:

EditText mEdit;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mEdit = (EditText)findViewById(R.id.edittext);

    callAPI();
}

public void callAPI(){

   /** Do your API call and retrieve the value/id **/

   mEdit.setText("value from API")
}

First things first. You need to use network libraries such as Retrofit, Volley, etc. for fetching JSON content from the server through an API. Retrofit is faster than Volley. Use the POST method to post the id to the server and from the API side you need to fetch data based on that id and convert it as JSON and send back the response.

Go through this link for further details. https://square.github.io/retrofit

If you are getting full JSON content, you need to parse through the list and get the JSON object related to the person id.

In the onClick, you can get the id by

// Probably check if it's a valid number and 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
    }
}
...

Since you are using Retrofit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM