简体   繁体   English

在Retrofit2中使用带有对象数组的json

[英]consume json with array of objects in retrofit2

I have this json I want to consume using the retrofit only that is not working very well 我有这个JSON我想使用改造来消耗,但效果不佳

{
"point_period_data": [
    [
        {
            "sys_point_id": "60",
            "sys_point_registration": "12345678",
            "sys_point_day": "21",
            "sys_point_month": "12",
            "sys_point_year": "18",
            "sys_point_hour": "08",
            "sys_point_minute": "05",
            "sys_point_geo_lat": "-5.0787026",
            "sys_point_geo_long": "-42.8044331",
            "sys_point_status": "Falta",
            "sys_point_reference_code": "dc4e6ff10a"
        },
        {
            "sys_point_id": "61",
            "sys_point_registration": "12345678",
            "sys_point_day": "21",
            "sys_point_month": "12",
            "sys_point_year": "18",
            "sys_point_hour": "17",
            "sys_point_minute": "28",
            "sys_point_geo_lat": "-5.0787569",
            "sys_point_geo_long": "-42.804378",
            "sys_point_status": "Falta",
            "sys_point_reference_code": "b11a829fda"
        },
        {
            "sys_point_id": "62",
            "sys_point_registration": "12345678",
            "sys_point_day": "21",
            "sys_point_month": "12",
            "sys_point_year": "18",
            "sys_point_hour": "17",
            "sys_point_minute": "28",
            "sys_point_geo_lat": "-5.0787571",
            "sys_point_geo_long": "-42.8043877",
            "sys_point_status": "Falta",
            "sys_point_reference_code": "a330347242"
        }
    ],
    [
        {
            "sys_point_id": "84",
            "sys_point_registration": "12345678",
            "sys_point_day": "22",
            "sys_point_month": "12",
            "sys_point_year": "18",
            "sys_point_hour": "02",
            "sys_point_minute": "59",
            "sys_point_geo_lat": "-5.0788858",
            "sys_point_geo_long": "-42.8043689",
            "sys_point_status": "Presença",
            "sys_point_reference_code": "22f1d178ef"
        }
    ]
]
}

My interface is this way 我的界面就是这样

@FormUrlEncoded
@POST("point/list/")
Call<List<List<Value>>> listPointPeriod(@Field("token") String token,
                                  @Field("registration") String registration,
                                  @Field("from_date") String from_date,
                                  @Field("to_date") String to_date);

This is called 这就是所谓的

Call<List<List<Value>>> callPonto = api.listPontoPeriod(token, resultFuncionario.get(i).getOfficials_registration(), dtInicio, dtFim);
callPonto.enqueue(new Callback<List<List<Value>>>() {
    @Override
    public void onResponse(Call<List<List<Value>>> call, Response<List<List<Value>>> response) {
        Gson gson = new Gson();
        Type collectionType = new TypeToken<List<List<Ponto>>>(){}.getType();
        List<Ponto> ponto = gson.fromJson(response.body().get(0).toString(), collectionType);   }

    @Override
    public void onFailure(Call<List<List<Value>>> call, Throwable t) {
       Log.d("error", t.getMessage());
    }
});

Model Ponto 模型蓬托

public class Ponto {

    String sys_point_id, sys_point_registration, sys_point_day, sys_point_month, sys_point_year, sys_point_hour, sys_point_minute,
        sys_point_geo_lat, sys_point_geo_long, sys_point_status, sys_point_reference_code;

    public Ponto(String sys_point_id, String sys_point_registration, String sys_point_day, String sys_point_month, String sys_point_year, String sys_point_hour, String sys_point_minute, String sys_point_geo_lat, String sys_point_geo_long, String sys_point_status, String sys_point_reference_code) {
        this.sys_point_id = sys_point_id;
        this.sys_point_registration = sys_point_registration;
        this.sys_point_day = sys_point_day;
        this.sys_point_month = sys_point_month;
        this.sys_point_year = sys_point_year;
        this.sys_point_hour = sys_point_hour;
        this.sys_point_minute = sys_point_minute;
        this.sys_point_geo_lat = sys_point_geo_lat;
        this.sys_point_geo_long = sys_point_geo_long;
        this.sys_point_status = sys_point_status;
        this.sys_point_reference_code = sys_point_reference_code;
    }

    public Ponto(){

    }

    public String getSys_point_id() {
        return sys_point_id;
    }

    public String getSys_point_registration() {
        return sys_point_registration;
    }

    public String getSys_point_day() {
        return sys_point_day;
    }

    public String getSys_point_month() {
        return sys_point_month;
    }

    public String getSys_point_year() {
        return sys_point_year;
    }

    public String getSys_point_hour() {
        return sys_point_hour;
    }

    public String getSys_point_minute() {
        return sys_point_minute;
    }

    public String getSys_point_geo_lat() {
        return sys_point_geo_lat;
    }

    public String getSys_point_geo_long() {
        return sys_point_geo_long;
    }

    public String getSys_point_status() {
        return sys_point_status;
    }

    public String getSys_point_reference_code() {
        return sys_point_reference_code;
    }
}

my value class 我的价值阶层

public class Value {List<Ponto> point_period_data;public List<Ponto>getPointPeriod() {return point_period_data}}

As you saw above my code, I am trying to consume this json, I tried in several ways this was the last one I tried. 正如您在我的代码上方看到的那样,我正在尝试使用此json,我以多种方式尝试了这是我尝试的最后一个方法。 If someone has already had it and have some answer to solve this problem please let me know. 如果有人已经有了它并且有解决该问题的答案,请告诉我。

I can see two main issues in the code you provided: 我可以在您提供的代码中看到两个主要问题:

1. The return type of your Retrofit method is wrong 1. Retrofit方法的返回类型错误

Call<List<List<Value>>> listPointPeriod

With this, you are expecting a List of List in the Json of your HTTP response. 这样,您期望在HTTP响应的Json中有一个列表列表。 However, the JSON you provided is an object with the "point_period_data" attribute (which is a List of List). 但是,您提供的JSON是具有“ point_period_data”属性(这是列表的列表)的对象。

2. You're mixing two methods of parsing the body of your HTTP response 2.您混用了两种解析HTTP响应正文的方法

When using Retrofit, the most usual way to parse the body of your response is to use a "converter". 使用Retrofit时,解析响应主体的最常用方法是使用“转换器”。 In your case, you seem to be using Gson and should have a look to https://github.com/square/retrofit/tree/master/retrofit-converters/gson 在您的情况下,您似乎正在使用Gson,请查看https://github.com/square/retrofit/tree/master/retrofit-converters/gson
The official documentation also shows how to add a Converter: https://square.github.io/retrofit/#restadapter-configuration 官方文档还显示了如何添加转换器: https : //square.github.io/retrofit/#restadapter-configuration

If you correctly configure Gson with Retrofit, you won't need to manually parse the result in onResponse 如果您使用Retrofit正确配置了Gson,则无需在onResponse手动解析结果

A corrected version would be something like: 更正后的版本将如下所示:

public class Value {
    List<List<Ponto>> point_period_data;

    public List<List<Ponto>> getPointPeriodData() {
        return point_period_data;
    }
}
@FormUrlEncoded
@POST("point/list/")
Call<Value> listPointPeriod(@Field("token") String token,
                                  @Field("registration") String registration,
                                  @Field("from_date") String from_date,
                                  @Field("to_date") String to_date);
Call<Value> callPonto = api.listPontoPeriod(token, resultFuncionario.get(i).getOfficials_registration(), dtInicio, dtFim);
            callPonto.enqueue(new Callback<Value>() {
                @Override
                public void onResponse(Call<Value> call, Response<Value> response) {
                    // Check response.isSuccessful and use response.body
                }

                @Override
                public void onFailure(Call<List<List<Value>>> call, Throwable t) {
                   Log.d("error", t.getMessage());
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM