2016-10-10 54 views
0

我試圖從https://newsapi.org/v1/articles的Retrofit獲得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); 
    } 
} 
+0

如果我記得,您只能從'onResponse'方法中使用'response.body()' –

+0

您使用的是proguard嗎? –

+0

另外,如果我閱讀正確的話,'@Query(「api_Key」)'應該是'apiKey'。 https://newsapi.org/ –

回答

1

從我可以告訴,我已經成功地建立成功的連接

好了,你在這裏忽略的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問題,而不是改造)

最後,我記得讀書的地方,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()); 
    } 
}); 

祝你好運!

+1

謝謝,我現在正在工作!我還成功實現了JSON數組映射的缺失'Articles'類,它通過在'NewsAPI'類的相應變量上使用'ArrayList '來實現。只是把它留在這裏供將來參考。 – KaiZ

+0

嗨Kalz我跟進到這一點你是如何得到響應ArrayList ,我使用相同的API –