2016-10-10 42 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 –

相关问题