android - 如何使用 Retrofit 从 json 中获取数据作为列表
问题描述
获取了单个数据,但是当我尝试以列表的形式获取时,它显示了问题Expected BEGIN_ARRAY 但是 BEGIN_OBJECT 在第 1 行第 2 列路径 $ 这里是 JSON 链接:https ://newsapi.org/v2/top-headlines?sources =abc-news&apiKey=4969854e2d424ed9972370f709ace9cc
这是我在 MainActivity 中获取 JSON 数据的方法
private void loadJSON_Data() {
RetofitClient_SingleTone
retofitClient_singleTone=RetofitClient_SingleTone.getInstance();
Api api = retofitClient_singleTone.getApi();
Call<List<Response_Pojo>> call = api.getAllResponse();
call.enqueue(new Callback<List<Response_Pojo>>() {
@Override
public void onResponse(Call<List<Response_Pojo>> call,
Response<List<Response_Pojo>> response) {
if (response.isSuccessful()) {
Toast.makeText(context, "Success" + response.body().size(),
Toast.LENGTH_SHORT).show();
Log.e("Home - --", "onResponse: " + response.body());
swipeRefreshLayout.setRefreshing(false);
}
}
@Override
public void onFailure(Call<List<Response_Pojo>> call, Throwable t) {
Log.e("Home Failed - -", "Failed: " + t.getMessage());
Toast.makeText(context, "Faield - - " +t.getMessage(),
Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
});
}
这是我的 api.Class
public interface Api {
@GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
Call<List<Response_Pojo>> getAllResponse();
}
RetofitClient_SingleTone 类
public class RetofitClient_SingleTone {
private static final String BASE_URL = "https://newsapi.org/v2/";
private static RetofitClient_SingleTone mInstance;
private Retrofit retrofit;
private RetofitClient_SingleTone() {
retrofit=new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetofitClient_SingleTone getInstance(){
if (mInstance==null){
mInstance = new RetofitClient_SingleTone();
}
return mInstance;
}
public Api getApi(){
return retrofit.create(Api.class);
}
}
这是我的 POJO 课程
public class Response_Pojo {
@SerializedName("status")
@Expose
private String status;
@SerializedName("totalResults")
@Expose
private Integer totalResults;
@SerializedName("articles")
@Expose
private List<Article> articles = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getTotalResults() {
return totalResults;
}
public void setTotalResults(Integer totalResults) {
this.totalResults = totalResults;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public class Source {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Article {
@SerializedName("source")
@Expose
private Source source;
@SerializedName("author")
@Expose
private String author;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("url")
@Expose
private String url;
@SerializedName("urlToImage")
@Expose
private String urlToImage;
@SerializedName("publishedAt")
@Expose
private String publishedAt;
@SerializedName("content")
@Expose
private String content;
public Source getSource() {
return source;
}
public void setSource(Source source) {
this.source = source;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
解决方案
在 api 服务接口内,您的响应应该是Response_Pojo
类而不是List<Response_Pojo>
如下所示:
public interface Api {
@GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
Call<Response_Pojo> getAllResponse();
}
还需要相应地更改调用 API 代码。
MainActivity.class:
private void loadJSON_Data() {
RetofitClient_SingleTone
retofitClient_singleTone=RetofitClient_SingleTone.getInstance();
Api api = retofitClient_singleTone.getApi();
Call<Response_Pojo> call = api.getAllResponse();
call.enqueue(new Callback<Response_Pojo>() {
@Override
public void onResponse(Call<Response_Pojo> call,
Response<Response_Pojo> response) {
if (response.isSuccessful()) {
Toast.makeText(context, "Success" + response.body().getArticles().size(),
Toast.LENGTH_SHORT).show();
Log.e("Home - --", "onResponse: " + response.body());
swipeRefreshLayout.setRefreshing(false);
}
}
@Override
public void onFailure(Call<Response_Pojo> call, Throwable t) {
Log.e("Home Failed - -", "Failed: " + t.getMessage());
Toast.makeText(context, "Faield - - " +t.getMessage(),
Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
});
}
请相应地更新代码,我还没有将它编辑到 IDE 中。
通过onResponse方法中的以下代码从响应中获取列表:
@Override
public void onResponse(Call<Response_Pojo> call,
Response<Response_Pojo> response) {
if (response.isSuccessful()) {
list = response.body().getArticles() // get the list from here
}
推荐阅读
- ios - 如何在ios中通过FTP上传文件?
- authentication - 缓存身份验证调用
- mongodb - Admin-on-rest '编辑页面 - 不正确的元素错误 // 创建页面 - 元素不存在错误'
- identityserver4 - 任何使用 IdentityServer4 作为授权的 Api 资源的 Asp.Net 4.7.2 示例?
- ios - 具有覆盖占位符的自定义 UITextField 无法按预期工作
- python - 是否可以在 QtableWidget 的垂直标题上应用 LableHeaderView?
- amazon-web-services - 跨多个帐户运行 aws cli 的方法
- javascript - 如何从嵌套的对象数组中显示可点击的表格?
- python - Keras:如何从 LSTM 的几层中检索隐藏状态和/或单元状态?
- symfony - 使用 EntityType 获取其他实体中不存在的数据