[英]Why am I getting a null object?
我正在嘗試使用Retrofit從https://newsapi.org/v1/articles
獲取JSON數據,但是我對此並不陌生,到目前為止,它一直令人困惑。
據我所知,我已經成功建立了成功的聯系。 但是,由於某些原因,我還無法識別,Retrofit不會將數據從JSON字符串映射到我的NewsAPI
類。
我將發布當前正在使用的代碼,希望有人可以幫助我解決這個問題。 謝謝!
Activity.java
:
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String url = "https://newsapi.org";
NewsAPI_Interface client = NewsAPI_Adapter.createService(NewsAPI_Interface.class);
Call<List<NewsAPI>> call = client.getData("techcrunch", "APIkeygoeshere");
call.enqueue(new Callback<List<NewsAPI>>() {
@Override
public void onResponse(Call<List<NewsAPI>> call, Response<List<NewsAPI>> response) {
if (response.body() != null) {
tvTest.setText(response.body().toString());
for (NewsAPI news: response.body()) {
System.out.println(news.source + " (" + news.status + ")");
tvTest.setText(news.source + " (" + news.status + ")");
}
} else {
tvTest.setText("It's null, " + response.errorBody().toString() + ", " + call.request().url());
}
}
@Override
public void onFailure(Call<List<NewsAPI>> call, Throwable t) {
tvTest.setText("An error ocurred!");
}
});
NewsAPI.java
:
public class NewsAPI {
String status;
String source;
public NewsAPI (String status, String source) {
this.status = status;
this.source = source;
}
}
NewsAPI_Interface.java
:
public interface NewsAPI_Interface {
@GET("/v1/articles")
Call<List<NewsAPI>> getData(@Query("source") String source, @Query("api_Key") String api_key);
}
NewsAPI_Adapter.java
:
public class NewsAPI_Adapter {
public static final String API_BASE_URL = "https://newsapi.org";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
據我所知,我已經成功建立了成功的聯系
好吧,您將忽略此處的Throwable,將在此處輸出錯誤。
@Override
public void onFailure(Call<List<NewsAPI>> call, Throwable t) {
tvTest.setText("An error ocurred!");
}
您期望的響應如下所示
{
"status": "ok",
"source": "techcrunch",
...
{
在那里開始一個對象 ,而不是List
,因此,您應該以不同的方式定義接口。 然后文檔說API密鑰是使用apiKey
添加到URL的,因此進行更改。
public interface NewsAPI_Interface {
@GET("/v1/articles")
Call<NewsAPI> getData(@Query("source") String source, @Query("apiKey") String api_key);
}
(您需要為Article
類定義另一個對象。但這是Gson問題,而不是Retrofit)
最后,我記得在某處讀過response.body()
只能調用一次。
Call<NewsAPI> call = client.getData("techcrunch", "APIkeygoeshere");
call.enqueue(new Callback<NewsAPI>() {
@Override
public void onResponse(Call<List<NewsAPI>> call, Response<List<NewsAPI>> response) {
final NewsAPI responseBody = response.body();
if (responseBody != null) {
tvTest.setText(responseBody.toString());
String news = news.source + " (" + news.status + ")";
Log.i("responseBody", news);
tvTest.setText(news);
} else {
tvTest.setText("It's null, " + response.errorBody().toString() + ", " + call.request().url());
}
}
@Override
public void onFailure(Call<List<NewsAPI>> call, Throwable t) {
tvTest.setText("An error ocurred!");
Log.e("news Error", t.getMessage());
}
});
祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.