【发布时间】: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