java - 在 retrofit2 中使用带有对象数组的 json

标签 java android json object retrofit2

我有这个 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"
        }
    ]
]
}

我的界面是这样的

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

这叫做

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

模型 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;
    }
}

我的值(value)观

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

正如您在我的代码上方看到的那样,我正在尝试使用此 json,我尝试了多种方式,这是我尝试的最后一种方式。如果有人已经有了它并且有解决这个问题的答案,请告诉我。

最佳答案

我可以在您提供的代码中看到两个主要问题:

1。你的 Retrofit 方法的返回类型是错误的

Call<List<List<Value>>> listPointPeriod

这样,您就可以在 HTTP 响应的 Json 中得到一个列表列表。但是,您提供的 JSON 是一个具有“point_period_data”属性的对象(这是一个列表的列表)。

2。您正在混合两种解析 HTTP 响应正文的方法

使用 Retrofit 时,解析响应主体的最常用方法是使用“转换器”。在你的情况下,你似乎正在使用 Gson 并且应该看看 https://github.com/square/retrofit/tree/master/retrofit-converters/gson
官方文档还展示了如何添加转换器:https://square.github.io/retrofit/#restadapter-configuration

如果你使用 Retrofit 正确配置 Gson,你将不需要手动解析 onResponse 中的结果

更正后的版本应该是这样的:

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

关于java - 在 retrofit2 中使用带有对象数组的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54282678/

相关文章:

java - 如何从 android 应用程序打开 "save to android pay"url?

java - JSON 解析错误 : Already had POJO for id

json - MarkLogic 是否原生存储 JSON?

java - 如何将 JSONObject 转换为 ArrayList

java - 为什么使用 EasyMock 时会抛出 NullPointerException

java - JVM JIT 编译器如何优化 "duplicated"Java 代码?

java - 移位以获得余数

java - 如何避免我的 SVN 嵌入式 $Id$ 属性出现 "line is longer than 80 characters"?

java - ImageView 中的位图被裁剪出屏幕

android - Firestore 快照监听器。在影响性能之前我可以听多少