【发布时间】:2020-08-25 18:26:01
【问题描述】:
我有 API 新闻 它由元素组成 网站名称、密钥、国家 一切都很好,我首先测试了链接: https://newsapi.org/v2/top-headlines?country=eg&apiKey=e80f949de1a34b94804188af28f08f44 然后我构建了所需的类,并设置了一个接口并将其放入 Headers , Query ,当我调用它时,我发现了一个错误。
///
public class RetrofitClient {
private static Retrofit retrofit;
private static final String BASE_URL = "https://newsapi.org/";
public static Retrofit getRetrofitInstance(){
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
///-----
public interface GetData {
String API_KEY="e80f949de1a34b94804188af28f08f44";
@Headers("X-Api-Key:"+API_KEY)
@GET("/v2/top-headlines")
Call<List<Response>>getAllHeadlines(@Query("country") String country);
}
//- MainActivity
GetData service = RetrofitClient.getRetrofitInstance().create(GetData.class);
Call<List<Response>> call = service.getAllHeadlines("eg");
call.enqueue(new Callback<List<Response>>(){
@Override
public void onResponse(Call<List<Response>> call, retrofit2.Response<List<Response>> response) {
Log.d("print","Don"+response.body());
}
@Override
public void onFailure(Call<List<Response>> call, Throwable t) {
Log.d("print","nON"+t.getMessage());
}
});
// Log.d
D/print: nONExpected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
【问题讨论】:
-
我希望这是一个免费的 api 密钥,而不是你实际收费的,在后一种情况下你真的不应该分享它。