Android改装NullPointerException

时间:2018-07-25 14:05:14

标签: android nullpointerexception retrofit

我的问题是,我的改装请求中总是出现nullpointerexception,但是我不知道为什么...

Feed是一个自己创建的类,其中应包含改装请求的响应。

ServiceAPI:

@GET("top-headlines?country=de&apiKey=xxx")
Call<Feed> getNewsFeed();

onCreateView中进行复古通话:

Retrofit retrofitNewsData = new Retrofit.Builder()
            .baseUrl(NetworkService.NEWS_DATA_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Log.d(TAG, "network service connected to: " + retrofitNewsData);

    ServiceRepository serviceRepositoryNewsData = retrofitNewsData.create(ServiceRepository.class);

    Call<Feed> callNews = serviceRepositoryNewsData.getNewsFeed();
    callNews.enqueue(new Callback<Feed>() {
        @Override
        public void onResponse(Call<Feed> call, Response<Feed> response) {
             newsFeed = response.body();
            Log.d(TAG, "Habe den feed: " + newsFeed + " erhalten." + response.code());
        }

        @Override
        public void onFailure(Call<Feed> call, Throwable t) {

        }
    });`

基本URL: public static final String NEWS_DATA_BASE_URL = "https://newsapi.org/v2/";

1 个答案:

答案 0 :(得分:-1)

您需要使用其他类型。如果您的正文可以为空,则应使用Response,然后解析响应正文。