Android Java 预计 begin_object 但 begin_array
Android Java expected begin_object but was begin_array
请帮助我
这是我的模特
个案class
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;
}
}
死亡人数class
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
}
测试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;
}
}
响应class
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模型class
public class Covid19模型 {
@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();
}
MainActivityclass
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”)
我无法找出这些代码中的问题所在,数据没有响应而是给出错误
正如您在 JSON 响应中所见,错误和参数以列表的形式出现。
所以请更改字段以在 Covid19Model 中列出
@SerializedName("parameters")
@Expose
private List<Parameters> parameters;
@SerializedName("errors")
@Expose
private List<Errors> errors;
请帮助我
这是我的模特
个案class
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;
}
}
死亡人数class
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
}
测试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;
}
}
响应class
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模型class
public class Covid19模型 {
@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();
}
MainActivityclass
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”)
我无法找出这些代码中的问题所在,数据没有响应而是给出错误
正如您在 JSON 响应中所见,错误和参数以列表的形式出现。
所以请更改字段以在 Covid19Model 中列出
@SerializedName("parameters")
@Expose
private List<Parameters> parameters;
@SerializedName("errors")
@Expose
private List<Errors> errors;