I am having trouble parsing the values and displaying in the recyclerview
made the Pojo
class's in jsonschema2pojo
, but when i run the app, it shows the toast in the enqueue OnFailure()
, i tried multiple things but no success any help i think it can be about the expecting jsonArray
/jsonObject
thing ?
Thank you in advance.
I want to get the values inside the array results[]
Json response given below:
"success": true,
"metadata": {
"sort": "POPULARITY",
"total_products": 20,
"title": "Phones & Tablets",
"results": [
{
"sku": "1",
"name": "Samsung Galaxy S9",
"brand": "Samsung",
"max_saving_percentage": 30,
"price": 53996,
"special_price": 37990,
"image": "https://cdn2.gsmarena.com/vv/bigpic/samsung-galaxy-s9-.jpg",
"rating_average": 5
},
APIReponse pojo class:
public class APIReponse {
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("metadata")
@Expose
private Metadata metadata;
MetaData pojo class:
public class MetaData {
@SerializedName("sort")
@Expose
private String sort;
@SerializedName("total_products")
@Expose
private Integer totalProducts;
@SerializedName("title")
@Expose
private String title;
@SerializedName("results")
@Expose
private List<Result> results = null;
Result pojo class:
public class Result {
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("name")
@Expose
private String name;
@SerializedName("brand")
@Expose
private String brand;
@SerializedName("max_saving_percentage")
@Expose
private Integer maxSavingPercentage;
@SerializedName("price")
@Expose
private Integer price;
@SerializedName("special_price")
@Expose
private Integer specialPrice;
@SerializedName("image")
@Expose
private String image;
@SerializedName("rating_average")
@Expose
private Integer ratingAverage;
This is retrofit API interface:
@GET("search/phone/page/1/")
Call<List<Result>> getAllPhones();
Retrofit call methods:
Call<List<Result>> call = service.getAllPhones();
call.enqueue(new Callback<List<Result>>() {
@Override
public void onResponse(Call<List<Result>> call, Response<List<Result>> response) {
generatePhonesList(response.body());
}
@Override
public void onFailure(Call<List<Result>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
private void generatePhonesList(List<Result> phonesList){
recyclerView = findViewById(R.id.recyclerView);
adapter = new PhonesAdapter(phonesList,this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
You have to use your APIResponse Class as Result from your call
@GET("search/phone/page/1/")
Call<APIResponse> getAllPhones();
and you have to change your onResponse Method:
call.enqueue(new Callback<APIResponse>() {
@Override
public void onResponse(Call<APIResponse> call, Response<APIResponse> response) {
generatePhonesList(response.body().metadata.results);
}
@Override
public void onFailure(Call<APIResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});