1

Please Help me

This is my Models

Cases 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;
}

}

Deaths 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

}

Tests 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;
}

}

Response 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;
}

}

Covid19Model class

public class 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 interface

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 class

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 
        }
    });

}

}

My Api

What is the problem My error code ("expected begin_array but was begin_object")

I can't find out what the problem is in these codes and the data doesn't come in response and gives an error instead

3
  • 1. Don't expose API key; put dummy text instead. 2. please include Covid19Model class and make sure it contains a List of Response class. eg: class Covid19Model { @SerializedName("response") @Expose private List<Response> response; } Commented Dec 20, 2020 at 7:28
  • I added just to forget and add here Commented Dec 20, 2020 at 7:37
  • Please Show your answer with the code Commented Dec 20, 2020 at 7:41

1 Answer 1

0

As you can see in JSON response, errors and parameters are comes as List.

enter image description here

So please change fields to list in Covid19Model

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



@SerializedName("errors")
@Expose
private List<Errors> errors;
Sign up to request clarification or add additional context in comments.

4 Comments

now it gives erroru unable to resolve hostname covid-193.p.rapidapi.com no address associated hostname
unable to resolve hostname covid-193.p.rapidapi.com no address associated hostname
It's something related to the network. not related to the JSON structure.
Thanks bro, I had a wifi problem on my phone, the data came

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.