Skip to content Skip to sidebar Skip to footer

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 te

Solution 1:

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

publicclassDetail {

    @SerializedName("age")
    @ExposeprivateString age;
    @SerializedName("email")
    @ExposeprivateString email;
    @SerializedName("id")
    @ExposeprivateString id;
    @SerializedName("mobile")
    @ExposeprivateString mobile;
    @SerializedName("name")
    @ExposeprivateString name;

    publicStringgetAge() {
        return age;
    }

    publicvoidsetAge(String age) {
        this.age = age;
    }

    publicStringgetEmail() {
        return email;
    }

    publicvoidsetEmail(String email) {
        this.email = email;
    }

    publicStringgetId() {
        return id;
    }

    publicvoidsetId(String id) {
        this.id = id;
    }

    publicStringgetMobile() {
        return mobile;
    }

    publicvoidsetMobile(String mobile) {
        this.mobile = mobile;
    }

    publicStringgetName() {
        return name;
    }

    publicvoidsetName(String name) {
        this.name = name;
    }

}

MyResponse.java

publicclassMyResponse {

    @SerializedName("details")
    @ExposeprivateList<Detail> details = null;
@SerializedName("success")
@ExposeprivateInteger success;
@SerializedName("message")
@ExposeprivateString message;
publicIntegergetSuccess() {
return success;
}

publicvoidsetSuccess(Integer success) {
this.success = success;
}

publicStringgetMessage() {
return message;
}

publicvoidsetMessage(String message) {
this.message = message;
}
    publicList<Detail> getDetails() {
        return details;
    }

    publicvoidsetDetails(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;

    publicvoidgetDataForId(final String id){
        ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
        Call<MyResponse> call = apiInterface.getResponse();
        call.enqueue(newCallback<MyResponse>() {
            @OverridepublicvoidonResponse(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(newRunnable() {
                            @Overridepublicvoidrun() {//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();*/
                        }
                    }
                }
            }

            @OverridepublicvoidonFailure(Call<MyResponse> call, Throwable t) {

            }
        });

    }

hope this helped..Happy coding :)

Post a Comment for "Retrofit Android: How To Fetch Json Data Using Id And Display Into Textview"