java - Android Java 预期 begin_object 但是 begin_array
问题描述
请帮我
这是我的模特
案例类
public class Cases {
@SerializedName("new")////this key value from json
@Expose
private String _new;
@SerializedName("active")
@Expose
private Integer active;
@SerializedName("critical")
@Expose
private Integer critical;
@SerializedName("recovered")
@Expose
private Integer recovered;
@SerializedName("1M_pop")
@Expose
private String _1MPop;
@SerializedName("total")
@Expose
private Integer total;
public String getNew() {
return _new;
}
public void setNew(String _new) {
this._new = _new;
}
public Integer getActive() {
return active;
}
public void setActive(Integer active) {
this.active = active;
}
public Integer getCritical() {
return critical;
}
public void setCritical(Integer critical) {
this.critical = critical;
}
public Integer getRecovered() {
return recovered;
}
public void setRecovered(Integer recovered) {
this.recovered = recovered;
}
public String get1MPop() {
return _1MPop;
}
public void set1MPop(String _1MPop) {
this._1MPop = _1MPop;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
死亡等级
public class Deaths {
@SerializedName("new")
@Expose
private String _new;
@SerializedName("1M_pop")
@Expose
private String _1MPop;
@SerializedName("total")
@Expose
private Integer total;
public String getNew() {
return _new;
}
public void setNew(String _new) {
this._new = _new;
}
public String get1MPop() {
return _1MPop;
}
public void set1MPop(String _1MPop) {
this._1MPop = _1MPop;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
public class Errors {//this is empty class
}
public class Parameters {//this is empty class
}
测试类
public class Tests {
@SerializedName("1M_pop")
@Expose
private String _1MPop;
@SerializedName("total")
@Expose
private Integer total;
public String get1MPop() {
return _1MPop;
}
public void set1MPop(String _1MPop) {
this._1MPop = _1MPop;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
响应类
public class Response {
@SerializedName("continent")
@Expose
private String continent;
@SerializedName("country")
@Expose
private String country;
@SerializedName("population")
@Expose
private Integer population;
@SerializedName("cases")
@Expose
private Cases cases;
@SerializedName("deaths")
@Expose
private Deaths deaths;
@SerializedName("tests")
@Expose
private Tests tests;
@SerializedName("day")
@Expose
private String day;
@SerializedName("time")
@Expose
private String time;
public String getContinent() {
return continent;
}
public String getCountry() {
return country;
}
public Integer getPopulation() {
return population;
}
public Cases getCases() {
return cases;
}
public Deaths getDeaths() {
return deaths;
}
public Tests getTests() {
return tests;
}
public String getDay() {
return day;
}
public String getTime() {
return time;
}
}
Covid19模型类
公共类 Covid19Model {
@SerializedName("get")
@Expose
private String get;
@SerializedName("parameters")
@Expose
private Parameters parameters;
@SerializedName("errors")
@Expose
private Errors errors;
@SerializedName("results")
@Expose
private Integer results;
@SerializedName("response")
@Expose
private List<Response> response;
public String getGet() {
return get;
}
public void setGet(String get) {
this.get = get;
}
public Parameters getParameters() {
return parameters;
}
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
public Errors getErrors() {
return errors;
}
public void setErrors(Errors errors) {
this.errors = errors;
}
public Integer getResults() {
return results;
}
public void setResults(Integer results) {
this.results = results;
}
public List<Response> getResponse() {
return response;
}
public void setResponse(List<Response> response) {
this.response = response;
}
Covid19WebAPI 接口
public interface Covid19WebApi {
@Headers({
"x-rapidapi-host:covid-193.p.rapidapi.com",
"x-rapidapi-key:fb818f40c4msh9ed8e59abf0e867p11b3bfjsn0900d33b78ef"//this is my rapidapi key
})
@GET("statistics")
Call<Covid19Model> getData();
}
MainActivity 类
public class MainActivity extends AppCompatActivity {//This is my app MainActivity
List<Response> responses;
private static final String BASE_URL = "https://covid-193.p.rapidapi.com/";//this is covid api website
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());//this is convert json
Retrofit retrofit = builder.build();
Covid19WebApi covid19WebApi = retrofit.create(Covid19WebApi.class);
Call<Covid19Model> call = covid19WebApi.getData();//this is call api interfacee method
call.enqueue(new Callback<Covid19Model>() {
@Override
public void onResponse(Call<Covid19Model> call, Response<Covid19Model> response) {
responses = response.body().getResponse();
for (Object data:responses){
System.out.println(data);//This my error (expected begin_array but was begin_object )
}
}
@Override
public void onFailure(Call<Covid19Model> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage().toString(),Toast.LENGTH_LONG).show();//this is toast message failure
}
});
}
}
有什么问题我的错误代码(“预期为 begin_array 但为 begin_object”)
我无法找出这些代码中的问题,并且数据没有响应并给出错误
解决方案
推荐阅读
- jenkins - 如何将标题(用于jenkins crumb)添加到github webbook?
- c# - 仍然暂停/恢复并开始新游戏效果不佳。想不出我该怎么办?
- visual-studio-code - 使用 VsCode 远程调试 C++
- function - 如何在 Powershell 中调用 Scriptblock 作为函数参数
- python - 如何使用 SQLAlchemy 从 Azure DB 存储和检索 JSON
- c# - 如何以 Windows 形式可视化来自 c# 控制台应用程序的数据?
- excel - 用于 cghheck T-1 的工作日日期的 vbScript 代码
- laravel-5 - 如何使用命令在 laravel 中创建观察者
- sorting - 数据表:带有 HTML 元素的排序列
- python - 具有条件 cumsum 的自定义聚合