Пожалуйста, помогите мне
Это мои модели
Класс случаев
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")
не могу понять в чем проблема в этих кодах и данные в ответ не приходят а вместо этого выдает ошибку
Добавил просто чтобы забыть и добавить сюда
Пожалуйста, покажите свой ответ с кодом
Как вы можете видеть в ответе JSON, ошибки и параметры представлены в виде списка.
Поэтому, пожалуйста, измените поля на список в Covid19Model.
@SerializedName("parameters")
@Expose
private List<Parameters> parameters;
@SerializedName("errors")
@Expose
private List<Errors> errors;
теперь выдает ошибку, что невозможно разрешить имя хоста covid-193.p.rapidapi.com, имя хоста не связано с адресом.
не удалось разрешить имя хоста covid-193.p.rapidapi.com нет имени хоста, связанного с адресом
Это что-то связанное с сетью. не связан со структурой JSON.
Спасибо бро, у меня была проблема с вайфаем на телефоне, данные пришли
1. Не раскрывайте ключ API; вместо этого поместите фиктивный текст. 2. включите класс Covid19Model и убедитесь, что он содержит класс List of Response. например: class Covid19Model { @SerializedName("response") @Expose private List<Response> ответ; }