回调onResponse和onFailure未调用(retrofit2)

时间:2018-02-09 10:53:02

标签: android callback listener retrofit2

我有一个光标这是一个 id 来从服务器获取下一组结果,我尝试添加执行while while循环直到光标值达到0(结果结束),我意识到 onResponse onFailure 回调方法不会被调用一次我添加了do while循环,因此光标停留在初始值而没有达到0(不会调用来自api的 fetchResults )。 loadTwitterFriends 不断被调用,导致无限循环。我能知道为什么即使我设置监听器来等待响应,也不会触发回调方法?如果do while循环已删除(回调 onResponse 工作),则成功检索到光标值但是我无法在没有循环的情况下迭代结果。任何形式的帮助表示赞赏!

do {
loadTwitterFriends(new cursorListener() {
    @Override
    public void onFollowReceived(long cursor) {
            adapter.notifyDataSetChanged();

    }
});
}
while(getCursor() != 0);

带有回调方法的loadTwitterFriends()

private void loadTwitterFriends(final cursorListener listener) {
    MyTwitterApiClient myTwitterApiClient = new MyTwitterApiClient(twitterSession);
    myTwitterApiClient.getCustomTwitterService().list(loggedUserTwitterId,getCursor()).enqueue(new retrofit2.Callback<FriendsResponseModel>() {
        @Override
        public void onResponse(Call<FriendsResponseModel> call, Response<FriendsResponseModel> response) {
            Log.e("onResponselol", "Is response success" + response.isSuccessful());
            twitterFriends = fetchResults(response);
            getActivity().setTitle(String.valueOf(twitterFriends.size()) + " Following");


        setCursor(fetchResults3(response));
        Log.e("onResponseFlwing", "Cursor:" + getCursor());


        Log.e("onResponse", "twitterfriends:" + twitterFriends.size());
        listener.onFollowReceived(getCursor());

    }

    @Override
    public void onFailure(Call<FriendsResponseModel> call, Throwable t) {
        Log.e("onFailure", t.toString());
        listener.onFollowReceived(1);
    }


});

}

fetchResults

    private List<TwitterFriends> fetchResults(Response<FriendsResponseModel> response) {
    FriendsResponseModel responseModel = response.body();
    if (responseModel != null) {
        Log.e("lol","asd");
        return responseModel.getResults();
    }
    else{
        Log.e("lol","dsa");
        return Collections.emptyList();
    }
}

0 个答案:

没有答案