Android Retrofit使用API​​无法正常工作

时间:2018-05-20 17:24:55

标签: android retrofit

我正在开发一个Android项目,我正在使用改造来获取JSON数据。我的JSON看起来像这样。

{  
       "status":"ok",
       "count":1,
       "count_total":1,
       "pages":1,
       "posts":[  
          {  
             "id":4,
             "type":"post",
             "slug":"my news",
             "status":"publish",
             "title":"test news title goes here",
             "title_plain":"test news title goes here",
             "content":"<p>news title goes<\/span><\/p>\n",
             "date":"2018-05-12 20:00:51",
             "modified":"2018-05-12 20:00:51"
          }
       ]
    }

我的模型类-Article.java

public class Article {

@SerializedName("id")
@Expose
private Integer id;

@SerializedName("status")
@Expose
private String status;

@SerializedName("title")
@Expose
private String title;

@SerializedName("title_plain")
@Expose
private String titlePlain;

@SerializedName("content")
@Expose
private String content;

@SerializedName("date")
@Expose
private String date;


public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getTitlePlain() {
    return titlePlain;
}

public void setTitlePlain(String titlePlain) {
    this.titlePlain = titlePlain;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

}

我正在调用这样的改造方法,并且已经添加了一个用于加载的对话框。但我没有得到API的回应。 (当我在浏览器上打字时它非常有效)

       NewsService service = RetrofitClient.getRetrofitInstance().create(NewsService.class);
    Call<List<Article>> call = service.getNewestArticles();
    call.enqueue(new Callback<List<Article>>() {
        @Override
        public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
            dialog.dismiss();
            generateDataList(response.body());
        }

        @Override
        public void onFailure(Call<List<Article>> call, Throwable t) {

        }
    });


   private void generateDataList(List<Article> articles) {

    lstNews.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    lstNews.setLayoutManager(layoutManager);


    adapter = new ListNewsAdapter(articles,getActivity().getBaseContext());
    adapter.notifyDataSetChanged();
    lstNews.setAdapter(adapter);

    lstNews.setAdapter(adapter);
}

我是新来的改造。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你实际上并没有加载一个文章列表(数组),而是一个包含文章列表的对象,所以你需要2个模型类:

public class ArticleResponse {
    @SerializedName("status")
    @Expose
    private String status;

    @SerializedName("count")
    @Expose
    private int count;

    @SerializedName("count_total")
    @Expose
    private int countTotal;

    @SerializedName("pages")
    @Expose
    private int pages;

    @SerializedName("posts")
    @Expose
    private List<Article> articles;

    //getter/Setter
    //...
}

您的改装服务应该返回该ArticleResponse,然后您可以通过模型中的articles getter获取您的文章数组

NewsService service = RetrofitClient.getRetrofitInstance().create(NewsService.class);
    Call<ArticleResponse> call = service.getNewestArticles();
    call.enqueue(new Callback<ArticleResponse>() {
        @Override
        public void onResponse(Call<ArticleResponse> call, Response<ArticleResponse> response) {
            dialog.dismiss();
            generateDataList(response.body().getArticles());
        }

        @Override
        public void onFailure(Call<ArticleResponse> call, Throwable t) {
            Log.w( t );
        }
    });