使用改造从 api 获取数据时出错
Posted
技术标签:
【中文标题】使用改造从 api 获取数据时出错【英文标题】:Error while fetching the data from api using retrofit 【发布时间】:2020-07-20 17:27:09 【问题描述】:这是我的 json 格式的数据 我很困惑如何使用改造来获取嵌套数组/对象数据
[
"state": "Kerala",
"districtData": [
"district": "Thrissur",
"confirmed": 12,
"lastupdatedtime": "",
"delta":
"confirmed": 0
,
"district": "Alappuzha",
"confirmed": 3,
"lastupdatedtime": "",
"delta":
"confirmed": 0
,
"district": "Kasaragod",
"confirmed": 156,
"lastupdatedtime": "",
"delta":
"confirmed": 0
,
]
]
My Resopnse Class:--
public class StateDistrictResponse
private String state;
private DistrictDataModal[] districtData;
public String getState()
return state;
public void setState(String state)
this.state = state;
public DistrictDataModal[] getDistrictData()
return districtData;
public void setDistrictData(DistrictDataModal[] districtData)
this.districtData = districtData;
public StateDistrictResponse(String state, DistrictDataModal[] districtData)
this.state = state;
this.districtData = districtData;
DistrictDataModal的POJO CLASS/MODal CLass::-
public class DistrictDataModal
private String district;
private String confirmed;
private String lastupdatedtime;
private DeltaModal delta;
public String getDistrict()
return district;
public void setDistrict(String district)
this.district = district;
public String getConfirmed()
return confirmed;
public void setConfirmed(String confirmed)
this.confirmed = confirmed;
public String getLastupdatedtime()
return lastupdatedtime;
public void setLastupdatedtime(String lastupdatedtime)
this.lastupdatedtime = lastupdatedtime;
public DeltaModal getDelta()
return delta;
public void setDelta(DeltaModal delta)
this.delta = delta;
public DistrictDataModal(String district, String confirmed, String lastupdatedtime, DeltaModal delta)
this.district = district;
this.confirmed = confirmed;
this.lastupdatedtime = lastupdatedtime;
this.delta = delta;
这里是我的对象类 Delta
public class DeltaModal
private String confirmed;
public String getConfirmed()
return confirmed;
public void setConfirmed(String confirmed)
this.confirmed = confirmed;
public DeltaModal(String confirmed)
this.confirmed = confirmed;
这里是改造代码,我不明白如何获取对象的数据:-
private void getStateDistrict()
Retrofit retrofit= new Retrofit.Builder()
.baseUrl(Apis.ROOT)
.addConverterFactory(GsonConverterFactory.create())
.build();
Apis request=retrofit.create(Apis.class);
Call<StateDistrictResponse> call=request.getState_Distric_Wise();
call.enqueue(new Callback<StateDistrictResponse>()
@Override
public void onResponse(Call<StateDistrictResponse> call, Response<StateDistrictResponse> response)
if (response.isSuccessful())
try
StateDistrictResponse response1=response.body();
Toast.makeText(StateDistrict.this, ""+response1.getDistrictData()[i].getDelta().getConfirmed(), Toast.LENGTH_SHORT).show();
catch (Exception e)
Toast.makeText(StateDistrict.this, "Ex:--"+e.toString(), Toast.LENGTH_LONG).show();
else
Toast.makeText(StateDistrict.this, "SOrry", Toast.LENGTH_SHORT).show();
@Override
public void onFailure(Call<StateDistrictResponse> call, Throwable t)
Toast.makeText(StateDistrict.this, "SOrry"+t.toString(), Toast.LENGTH_SHORT).show();
);
【问题讨论】:
在 POJO 类变量中添加@Expose
和 @SerializedName("<param name>")
注释。
【参考方案1】:
对于这类复杂的 json。我建议使用this perfect tool 可以将您的json 转换为结构良好的POJO。不管json多么复杂。
希望对你有帮助??
【讨论】:
【参考方案2】:在您的 StateDistrictResponse 类中:
private DistrictDataModal[] districtData;
应该是:
private List<DistrictDataModal> districtDate;
并很好地对齐 POJO。我还注意到根对象是一个数组?因此,请务必仔细检查,因为 json 以方括号开头。
【讨论】:
【参考方案3】:这个链接可能有用:
点击here! 这很好用。
【讨论】:
以上是关于使用改造从 api 获取数据时出错的主要内容,如果未能解决你的问题,请参考以下文章
从 Angular 中的 REST API 获取 HTTPS 时出错