我有这个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());
}
});
蓬图模型
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;
}
}
我的价值阶层
public class Value {List<Ponto> point_period_data;public List<Ponto>getPointPeriod() {return point_period_data}}
正如您在我的代码上方看到的那样,我正在尝试使用此json,我以多种方式尝试了这是我尝试的最后一个方法。如果有人已经有了它并且有解决该问题的答案,请告诉我。
答案 0 :(得分:0)
在您提供的代码中,我可以看到两个主要问题:
Call<List<List<Value>>> listPointPeriod
有了这个,您期望在HTTP响应的Json中有一个列表列表。但是,您提供的JSON是具有“ point_period_data”属性(这是列表的列表)的对象。
使用Retrofit时,解析响应正文的最常用方法是使用“转换器”。就您而言,您似乎正在使用Gson,应该看看https://github.com/square/retrofit/tree/master/retrofit-converters/gson
官方文档还显示了如何添加Converter: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());
}
});