简体   繁体   中英

Retrofit Android: how to fetch Json data using id and display into textview

I want to fetch only particular data from json object using id and show it in textview. I am able fetch data in listview. But i want fetch data without using listview into basic textview. using each id.

**Only want Fetch id="1" json data and display it into textview name,mobile,age,email. After clicking button **

Json Data

{
"details": [
    {
        "age": "56",
        "email": "john@gmail.com",
        "id": "1",
        "mobile": "985323154",
        "name": "John"
    },
    {
        "age": "0",
        "email": "",
        "id": "26",
        "mobile": "0",
        "name": "1"
    }
]

}

DisplayData Class

public class DisplayData extends AppCompatActivity {

    String BASE_URL = "";


    ArrayList<String> id = new ArrayList<>();
    ArrayList<String> name = new ArrayList<>();
    ArrayList<String> age = new ArrayList<>();
    ArrayList<String> mobile = new ArrayList<>();
    ArrayList<String> email = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

       displayData();


    }

       public void displayData() {

        RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(BASE_URL) //Setting the Root URL
                .build();

        AppConfig.read api = adapter.create(AppConfig.read.class);

        api.readData(new Callback<JsonElement>() {
                         @Override
                         public void success(JsonElement result, Response response) {

                             String myResponse = result.toString();
                             Log.d("response", "" + myResponse);

                             try {
                                 JSONObject jObj = new JSONObject(myResponse);

                                 int success = jObj.getInt("success");

                                 if (success == 1) {

                                     JSONArray jsonArray = jObj.getJSONArray("details");
                                     for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject jo = jsonArray.getJSONObject(i);
                                        id.add(jo.getString("id"));
                                         name.add(jo.getString("name"));
                                         age.add(jo.getString("age"));
                                         mobile.add(jo.getString("mobile"));
                                         email.add(jo.getString("email"));


                                     }

                         }

                         @Override
                         public void failure(RetrofitError error) {
                             Log.d("Failure", error.toString());
                             Toast.makeText(DisplayData.this, error.toString(), Toast.LENGTH_LONG).show();
                         }
                     }
        );
    }
}

What i can make out from your code is you are having different arrays for all the fields, which is not a good way to store data, Do it this way: Make a bean of your attributes like this:

Detail.java

public class Detail {

    @SerializedName("age")
    @Expose
    private String age;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("mobile")
    @Expose
    private String mobile;
    @SerializedName("name")
    @Expose
    private String name;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

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

    public String getId() {
        return id;
    }

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

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

MyResponse.java

public class MyResponse {

    @SerializedName("details")
    @Expose
    private List<Detail> details = null;
@SerializedName("success")
@Expose
private Integer success;
@SerializedName("message")
@Expose
private String message;
public Integer getSuccess() {
return success;
}

public void setSuccess(Integer success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
    public List<Detail> getDetails() {
        return details;
    }

    public void setDetails(List<Detail> details) {
        this.details = details;
    }

}

your client builder

@GET("/retro/displayAll.php")
    Call<MyResponse> getResponse();//imp to include MyResponse as a call

Your Method to get Details

Detail reqDetail;

    public void getDataForId(final String id){
        ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
        Call<MyResponse> call = apiInterface.getResponse();
        call.enqueue(new Callback<MyResponse>() {
            @Override
            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                if(response.body() != null){
                    MyResponse myResponse = response.body();
                    List<Detail> details = myResponse.getDetails();
                    for(Detail d : details){
                        if(d.getId().equals(id)){
                          reqDetail = d;

                          runOnUiThread(new Runnable() {
                            @Override
                            public void run() {//update your views here
                                tvName.setText(reqDetail.getName());
                            }
                            });
                            /*reqDetail will be having everything that you need and you can get it using the following code.
                            reqDetail.getName();
                            reqDetail.getAge();
                            reqDetail.getEmail();
                            reqDetail.getMobile();*/
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<MyResponse> call, Throwable t) {

            }
        });

    }

hope this helped..Happy coding :)

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