首页 > 解决方案 > 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 
        }
    });

}

}

我的 API

有什么问题我的错误代码(“预期为 begin_array 但为 begin_object”)

我无法找出这些代码中的问题,并且数据没有响应并给出错误

标签: javaandroidmobileretrofit

解决方案


正如您在 JSON 响应中看到的,错误和参数以 List 的形式出现。

在此处输入图像描述

所以请更改字段以在 Covid19Model 中列出

@SerializedName("parameters")
@Expose
private List<Parameters> parameters;



@SerializedName("errors")
@Expose
private List<Errors> errors;

推荐阅读