Java 安卓系统中不解析的改型

Java 安卓系统中不解析的改型,java,android,json,retrofit,pojo,Java,Android,Json,Retrofit,Pojo,我在解析值并在jsonschema2pojo中创建的Pojo类中显示时遇到问题,但当我运行应用程序时,它会在队列OnFailure()中显示toast,我尝试了多种方法,但都没有成功。我认为这可能与预期的jsonArray/jsonObject有关 先谢谢你 我想获取数组中的值results[] Json响应如下: "success": true, "metadata": { "sort": "POPULARITY", "total_products": 20,

我在解析值并在
jsonschema2pojo
中创建的
Pojo
类中显示时遇到问题,但当我运行应用程序时,它会在队列
OnFailure()
中显示toast,我尝试了多种方法,但都没有成功。我认为这可能与预期的
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;
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;
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;

元数据pojo类:

public class APIReponse {

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

@获取(“搜索/电话/页面/1/”)
调用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);
    }


Call Call=service.getAllPhones();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
generatePhonesList(response.body());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(MainActivity.this,“出现问题…请稍后再试!”,Toast.LENGTH\u SHORT.show();
}
});
}
私有void generatePhonesList(列表电话列表){
recyclerView=findViewById(R.id.recyclerView);
适配器=新电话适配器(phonesList,this);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(适配器);
}

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

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
    }
}
类数据响应{
val成功:String=“”
val元数据:MetadataResponse?=null
类MetadataResponse{
val sort:String=“”
val total_产品:Int=0
val title:String=“”
val结果:List=arrayListOf()
类ItemResult{
val sku:String=“”
val name:String=“”
val brand:String=“”
val最大保存百分比:Int=0
val价格:Int=0
val特殊价格:Int=0
val image:String=“”
val额定值_平均值:Int=0
}
}
以及:

@GET(“search/phone/page/1/”)
调用getAllPhones();
以及:

Call Call=service.getAllPhones();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
val itemResults=response.metadata?.results
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(MainActivity.this,“出现问题…请稍后再试!”,Toast.LENGTH\u SHORT.show();
}
});
您应该使用APIReponse而不是Result

像这样

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

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

@GET("search/phone/page/1/")
Call<APIResponse> getAllPhones();
@GET(“search/phone/page/1/”)
调用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();

            }
      });
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
generatePhonesList(response.body().metadata.results);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(MainActivity.this,“出现问题…请稍后再试!”,Toast.LENGTH\u SHORT.show();
}
});

最好先使用POJO类。@ArbenMaloku谢谢,我做了更改!您能提供错误消息吗?Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH\u SHORT)。show();Hello@Botz是:应该是BEGIN_数组,但在第1行第2列path$是BEGIN_对象,json start是一个对象,我要解析的数据在数组中,所以我有点confused@vpedro72多亏了#Botzand,你可以查看我编辑的答案,也许没有列表,只需拨打getAllPhones();Hello@Botz谢谢你的帮助,我仍然有问题,我的onResponse不能正常工作我使用了你的代码,或者我尝试了任何更改我在使用类的调用方法之间有一些冲突,有什么建议吗?哦,是的,我的代码示例中有一个小错误,必须是Callback,在onFailure中必须是Call我在示例代码。如果OnResponse必须将调用作为ApiResponse调用,而响应作为ListResults调用,您能确认我吗?它们是冲突的,我为所有的麻烦感到抱歉!哦,对不起,当然不是,响应也必须来自APIResponsethanks类型,我应该更改generatePhonelist方法中的某些内容还是更改广告中的任何内容apter(“Iam在适配器中以列表的形式传递结果”),因为这一行:“generatePhonesList(response.body().metadata.Results)”;“Is not wotking我只能做response.body().getMetadata”,它会出错