我正在使用一个名为 CryptoCompare 的有关加密货币新闻的 API。 我的问题是我无法检测到我的代码错误是什么。
错误如下 -> com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期为 BEGIN_ARRAY,但在第 1 行第 70 列路径 $.Data 处为 BEGIN_OBJECT
我复制 Json 和我的两个类来帮助我找到解决方案。
Json:
{
"Type": 100,
"Message": "News list successfully returned",
"Promoted": [
],
"Data": [
{
"id": "2940487",
"guid": "https://cointelegraph.com/news/australian-hacker-pleads-guilty-to-stealing-450-000-in-xrp-last-year",
"published_on": 1566590880,
"imageurl": "https://images.cryptocompare.com/news/cointelegraph/dj0O90McM86.png",
"title": "Australian Hacker Pleads Guilty to Stealing $450,000 in XRP Last Year",
"url": "https://cointelegraph.com/news/australian-hacker-pleads-guilty-to-stealing-450-000-in-xrp-last-year",
"source": "cointelegraph",
"body": "An Australian woman has pleaded guilty to stealing $450,000 in XRP",
"tags": "Altcoin|Australia|Fraud|Hackers|XRP|Tokens|Police",
"categories": "XRP|ICO|Altcoin",
"upvotes": "0",
"downvotes": "0",
"lang": "EN",
"source_info": {
"name": "CoinTelegraph",
"lang": "EN",
"img": "https://images.cryptocompare.com/news/default/cointelegraph.png"
}
},
]
Api链接-> https://min-api.cryptocompare.com/data/v2/news/?lang=EN
Java 类新闻:
public class News {
@SerializedName("Type")
@Expose
private Integer type;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("Data")
@Expose
private List<Article> articles = null;
@SerializedName("HasWarning")
@Expose
private Boolean hasWarning;
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public Boolean getHasWarning() {
return hasWarning;
}
public void setHasWarning(Boolean hasWarning) {
this.hasWarning = hasWarning;
}
Java 类文章:
public class Article {
@SerializedName("id")
@Expose
private String id;
@SerializedName("guid")
@Expose
private String guid;
@SerializedName("published_on")
@Expose
private Integer publishedOn;
@SerializedName("imageurl")
@Expose
private String imageurl;
@SerializedName("title")
@Expose
private String title;
@SerializedName("url")
@Expose
private String url;
@SerializedName("source")
@Expose
private String source;
@SerializedName("body")
@Expose
private String body;
@SerializedName("tags")
@Expose
private String tags;
@SerializedName("categories")
@Expose
private String categories;
@SerializedName("upvotes")
@Expose
private String upvotes;
@SerializedName("downvotes")
@Expose
private String downvotes;
@SerializedName("lang")
@Expose
private String lang;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public Integer getPublishedOn() {
return publishedOn;
}
public void setPublishedOn(Integer publishedOn) {
this.publishedOn = publishedOn;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getCategories() {
return categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public String getUpvotes() {
return upvotes;
}
public void setUpvotes(String upvotes) {
this.upvotes = upvotes;
}
public String getDownvotes() {
return downvotes;
}
public void setDownvotes(String downvotes) {
this.downvotes = downvotes;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}
调用Api的接口(interface):
public interface ApiInterface {
@GET("news")
Call<News> getNews(
@Query("lang") String lang,
@Query("api_key") String apiKey,
@Query("lTs") int lTs
);
改造类生成器
public static Retrofit getApiClient(String BASE_URL){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.client(getUnsafeOkHttpClient().build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
调用 api 时的代码 fragment
private void LoadJson(){
swipeRefreshLayout.setRefreshing(true);
final ApiInterface apiInterface = ApiClient.getApiClient(ApiUtils.BASE_URL_NEWS).create(ApiInterface.class);
Call<News> call;
call = apiInterface.getNews("EN", ApiUtils.API_KEY,0);
call.enqueue(new Callback<News>() {
@Override
public void onResponse(Call<News> call, Response<News> response) {
if (response.isSuccessful() && response.body() != null){
articles.addAll(response.body().getArticles());
if (articles.size() - response.body().getArticles().size() == 0){
adapterNews.notifyDataSetChanged();
} else {
adapterNews.notifyItemRangeInserted(articles.size() - response.body().getArticles().size(), response.body().getArticles().size());
}
swipeRefreshLayout.setRefreshing(false);
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(getContext(), "No result", Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
progressBar.setVisibility(View.GONE);
}
}
@Override
public void onFailure(Call<News> call, Throwable t) {
Log.e(TAG, "ERROR API: " + t.getMessage() + " - " + t.getCause());
}
});
}
任何贡献都非常有帮助。
谢谢
解决了通话中的问题
最佳答案
我注意到,当您尝试使用这样的错误值查询 api 时
https://min-api.cryptocompare.com/data/v2/news/?lang=en
为您提供json
而不是数据的array
,因此这会产生错误
要修复和测试,而不是 Locale.getDefault().getLanguage()
,请仅使用“EN”并检查结果
如果需要更多帮助,您可以使用此 logging-interceptor
关于java - 使用 Gson 和改造的 Java 的 Json 错误 [预期为 BEGIN_ARRAY,但在第 1 行第 70 列路径 $.Data 处为 BEGIN_OBJECT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57638174/