你好,我得到下面的错误,得到错误说预期的数组,但对象我也提供的网址,请指导......不熟悉json解析刚开始无法理解的错误,如果可能的话,请解释与解决方案。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
ServerUrlhttps:/api.rootnet.incovid19-instatslatest。
要求
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Code",response);
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
UnofficialSummary[] data = gson.fromJson(response,UnofficialSummary[].class);
recyclerView.setAdapter(new cronatracker(RSSFeedActivity.this,data));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue= Volley.newRequestQueue(this);
queue.add(request);
Unofficialsummary.class
public class UnofficialSummary {
@SerializedName("source")
@Expose
private String source;
@SerializedName("total")
@Expose
private Integer total;
@SerializedName("recovered")
@Expose
private Integer recovered;
@SerializedName("deaths")
@Expose
private Integer deaths;
@SerializedName("active")
@Expose
private Integer active;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getRecovered() {
return recovered;
}
public void setRecovered(Integer recovered) {
this.recovered = recovered;
}
public Integer getDeaths() {
return deaths;
}
public void setDeaths(Integer deaths) {
this.deaths = deaths;
}
public Integer getActive() {
return active;
}
public void setActive(Integer active) {
this.active = active;
}
}
响应数据.类
public class ResponseData {
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("data")
@Expose
private Data data;
@SerializedName("lastRefreshed")
@Expose
private String lastRefreshed;
@SerializedName("lastOriginUpdate")
@Expose
private String lastOriginUpdate;
/* Getters and setters */
}
Data.class
public class Data {
@SerializedName("summary")
@Expose
private Summary summary;
@SerializedName("unofficial-summary")
@Expose
private List<UnofficialSummary> unofficialSummary = null;
@SerializedName("regional")
@Expose
private List<Regional> regional = null;
/* Getters and setters */
}
Regional.class
public class Regional {
@SerializedName("confirmedCasesIndian")
@Expose
private Integer confirmedCasesIndian;
@SerializedName("confirmedCasesForeign")
@Expose
private Integer confirmedCasesForeign;
@SerializedName("discharged")
@Expose
private Integer discharged;
@SerializedName("deaths")
@Expose
private Integer deaths;
@SerializedName("loc")
@Expose
private String loc;
@SerializedName("totalConfirmed")
@Expose
private Integer totalConfirmed;
/* Getters and setters */
}
Summary.class
public class Summary {
@SerializedName("total")
@Expose
private Integer total;
@SerializedName("confirmedCasesIndian")
@Expose
private Integer confirmedCasesIndian;
@SerializedName("confirmedCasesForeign")
@Expose
private Integer confirmedCasesForeign;
@SerializedName("discharged")
@Expose
private Integer discharged;
@SerializedName("deaths")
@Expose
private Integer deaths;
@SerializedName("confirmedButLocationUnidentified")
@Expose
private Integer confirmedButLocationUnidentified;
/* Getters and setters */
}
UnofficialSummary.class
public class UnofficialSummary {
@SerializedName("source")
@Expose
private String source;
@SerializedName("total")
@Expose
private Integer total;
@SerializedName("recovered")
@Expose
private Integer recovered;
@SerializedName("deaths")
@Expose
private Integer deaths;
@SerializedName("active")
@Expose
private Integer active;
/* Getters and setters */
}
onResponse方法
public void onResponse(String response) {
Log.d("Code",response);
Gson gson = new Gson();
ResponseData data = gson.fromJson(response,ResponseData.class);
if(data.getSuccess()){
recyclerView.setAdapter(new cronatracker(RSSFeedActivity.this,data.getData().getUnofficialSummary()));
}
}