改造未在 Android 中解析

Retrofit not parsing in Android

我在解析 recyclerview 中的值和显示时遇到问题,使 Pojo class 出现在 jsonschema2pojo 中,但是当我 运行该应用程序,它在排队 OnFailure() 中显示吐司,我尝试了多种方法但没有成功任何帮助我认为这可能与预期的 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
      },

API回复 pojo class:

public class APIReponse {

    @SerializedName("success")
    @Expose
    private Boolean success;
    @SerializedName("metadata")
    @Expose
    private 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;

结果 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;

这是改装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);
    }

您需要创建数据class 相同:

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

        }
  });

You should use APIReponse instead of Result class

像这样。

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

您必须使用 APIResponse Class 作为调用的结果

@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, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();

            }
      });