【问题标题】:Retrofit not getting data from the KEY改造未从 KEY 获取数据
【发布时间】:2021-08-30 19:01:26
【问题描述】:

我使用了另一个测试 API,它工作正常,但是当我使用带有我的 KEY 的实际 API 时,它没有获取任何数据。

我的 API 服务

public interface APIServiceTop {

@GET("v2/top-headlines")
Call<List<ArticlesModel>> getTopObjectsList(
        @Query("country")
        String countryCode,
        @Query("page")
        int pageNumber,
        @Query("apikey")
        String API_KEY
    );
}

我的 RetroInstance

public class RetroInstance {

public static String BASE_URL = "https://newsapi.org/";

private static Retrofit retrofit;
public static HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

public static Retrofit getRetroClientTop() {
    logging.level(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(logging)
            .build();

    if(retrofit == null ) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
        return retrofit;
    }

这是来自文档的链接,我按照他们写的那样制作。 document

【问题讨论】:

  • 我建议您将日志记录添加到您的改造实例中。通过这种方式,您可以准确地看到您从服务器获得的响应以及您是否收到任何响应。如果你不知道怎么做,这里有解释的链接:stackoverflow.com/questions/21886313/…你也有可能超过了api请求的每日限制。
  • 谢谢,我添加了它,但它也没有得到任何数据。我已将我的课程更新为新课程
  • 所以,您没有看到任何响应数据,但您收到的是响应代码 200,对吧?
  • 在我的日志中首先显示此节目 2021-06-14 19:10:02.977 12585-12682/com.moataz.mox D/OkHttp: newsapi.org/v2/… (902ms)。之后,所有数据都显示在 logcat 中。是的,recyclerView 中没有数据显示
  • 我已经在github上发布了这个项目。 github.com/MoatazBadawy/MOX

标签: java android


【解决方案1】:

您的模型类中存在问题。试试下面的代码。

//Update your service class code
public interface APIServiceTop {

@GET("v2/top-headlines")
Call<ArticlesModel> getTopObjectsList(
        @Query("country")
        String countryCode,
        @Query("apikey")
        String API_KEY
);
}

//main model
public class ArticlesModel {
private String status;
private int totalResults;
private ArrayList<Articles> articles;
}

public class Articles {

private String urlToImage;
private String title;
private String details;
private String pages;
private String author;
private String content;
private String url;

public Articles(String urlToImage, String title, String description, String name, String author, String content, String url) {
    this.urlToImage = urlToImage;
    this.title = title;
    this.details = description;
    this.pages = name;
    this.author = author;
    this.content = content;
    this.url = url;
}

public String getUrlToImage() {
    return urlToImage;
}

public void setUrlToImage(String urlToImage) {
    this.urlToImage = urlToImage;
}

public String getTitle() {
    return title;
}

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

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public String getPages() {
    return pages;
}

public void setPages(String pages) {
    this.pages = pages;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getContent() {
    return content;
}

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

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
}

【讨论】:

  • 在 Aticles.java 类中您缺少 Source 对象。
  • 另外,返回类型应该是 Call,而不是 Call>
  • 感谢你们的真正帮助。现在可以工作了。对于任何会看到这个问题的人来说,另一个提示是,您可以使用插件名称“json to file”,只需复制您的 json 文件并制作新的 kotlin json 文件,然后在您的班级中过去。然后它会为你创建所有的类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2017-12-19
  • 2022-01-21
  • 2021-10-17
  • 2020-07-20
  • 2021-09-29
  • 2020-08-14
相关资源
最近更新 更多