翻新api调用的多个请求不起作用

时间:2019-05-06 17:20:17

标签: android rest mvvm retrofit2 android-livedata

我正在尝试使用改造功能来调用多个api请求,但它无法正常工作,并且不会返回始终返回null的响应。

有人可以帮助我解决此问题吗?

如何对500个请求进行api调用?

public class CommentRepository {
    private NewsService mNewsService;
    private static CommentRepository mCommentRepository;
    private MutableLiveData<List<Story>> mStoryListObservable = new MutableLiveData<>();
    private Story story;

    public CommentRepository(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(NewsService.HTTPS_API)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mNewsService = retrofit.create(NewsService.class);
    }

    private synchronized static CommentRepository getInstance(){
        if (mCommentRepository == null) {
            mCommentRepository = new CommentRepository();
        }
        return mCommentRepository;
    }

    public MutableLiveData<List<Story>> getTopStoryList() {
        List<Story> storyArrayList = new ArrayList<>();
        mNewsService.getTopStories().enqueue(new Callback<List<Long>>() {
            @Override
            public void onResponse(Call<List<Long>> call, Response<List<Long>> response) {
                List<Long> storyList = response.body();
                        for (int i = 0; i < storyList.size(); i++) {
                            Story storyDetails = getStoryDetails((long) i);
                            if (storyDetails != null)
                                storyArrayList.add(storyDetails);
                        }
                mStoryListObservable.postValue(storyArrayList);
            }

            @Override
            public void onFailure(Call<List<Long>> call, Throwable t) {
                //TODO: Need to Handle Error
            }
        });
        return mStoryListObservable;
    }

    private synchronized Story getStoryDetails(Long storyItem) {
        mNewsService.getStoryItem(String.valueOf(storyItem)).
                enqueue(new Callback<Story>() {
                    @Override
                    public void onResponse(Call<Story> call, Response<Story> response) {
                        simulateDelay();
                        story = response.body();
                    }

                    @Override
                    public void onFailure(Call<Story> call, Throwable t) {
                        // TODO better error handling
                        //storyData.setValue(null);
                    }
                });
        return story;
    }
}

应返回上述代码的500个api请求结果

0 个答案:

没有答案