gpt4 book ai didi

java - Volley 预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径处为 BEGIN_OBJECT

转载 作者:行者123 更新时间:2023-12-01 16:20:40 30 4
gpt4 key购买 nike

您好,我收到以下错误,收到错误说预期的数组,但是对象我还提供了网址请指导...不熟悉json解析刚刚开始无法理解错误如果可能请解释解决方案。

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

服务器网址 https://api.rootnet.in/covid19-in/stats/latest

请求

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

非官方摘要.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;
}

}

最佳答案

ResponseData.class

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 */

}

数据.类

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 */

}

区域.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 */

}

摘要.类

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

关于java - Volley 预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径处为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62292608/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com