无法在Android中解析翻新版

我在解析值并在recyclerview中显示时遇到了麻烦,使得Pojo中的jsonschema2pojo类位于OnFailure()中,但是当我运行该应用程序时,它在队列{{ 1}},我尝试了很多事情,但是没有成功,我认为这可能与期望的jsonArray / jsonObject事情有关?

谢谢。

我想获取数组results[]内的值

Json响应如下:

  "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类:

public class APIReponse {

    @SerializedName("success")
    @Expose
    private Boolean success;
    @SerializedName("metadata")
    @Expose
    private Metadata metadata;

MetaData pojo类:

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;

结果pojo类:

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;

这是改造API接口:


    @GET("search/phone/page/1/")
    Call<List<Result>> getallPhones(); 

改造调用方法:


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

gxwmwy 回答:无法在Android中解析翻新版

您需要创建相同的数据类:

class DataResponse {
val success: String = ""
val metadata: MetadataResponse? = null

class MetadataResponse {
    val sort: String = ""
    val total_products: Int = 0
    val title: String = ""
    val results: List<ItemResult> = arrayListOf()
    class ItemResult {
        val sku: String = ""
        val name: String = ""
        val brand: String = ""
        val max_saving_percentage: Int = 0
        val price: Int = 0
        val special_price: Int = 0
        val image: String = ""
        val rating_average: Int = 0
    }
}

并且:

@GET("search/phone/page/1/")
Call<DataResponse> getAllPhones(); 

并且:

Call<DataResponse> call = service.getAllPhones();

    call.enqueue(new Callback<DataResponse>() {
        @Override
        public void onResponse(Call<DataResponse> call,Response<DataResponse> response) {
            val itemResults = response.metadata?.results
        }

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

            Toast.makeText(MainActivity.this,"Something went wrong...Please try later!",Toast.LENGTH_SHORT).show();

        }
  });
,
  

您应使用 APIReponse 而不是 Result

赞。

@GET("search/phone/page/1/")
Call<APIReponse> getAllPhones(); 
,

您必须使用API​​Response类作为调用的结果

@GET("search/phone/page/1/")
Call<APIResponse> getAllPhones();

,您必须更改onResponse方法:

      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,Toast.LENGTH_SHORT).show();

            }
      });
本文链接:https://www.f2er.com/2898347.html

大家都在问