完成Retrofit调用后如何调用方法?

时间:2017-12-12 17:47:45

标签: android retrofit

我使用This library来显示来自API的配置文件,但我的问题是bubblePicker.setAdapter()在Retrofit调用完成之前执行。

我认为这是因为enqueue()是异步方法,所以它会在不等待的情况下执行。我不知道如何解决这个问题。

我的代码将抛出NullPointerException,因为studentsArray为空。

请提供一些建议。 感谢

我的代码:

public class FriendFragment extends Fragment {

    Http http = new Http();
    final String BASE_URL = http.getUrl();

    String[] studentsArray;
    List < Student > students;

    BubblePicker bubblePicker;


    public static FriendFragment newInstance() {
        FriendFragment fragment = new FriendFragment();
    return fragment;
    }

    private ApiService getInterfaceService() {
        Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
        final ApiService mInterfaceService = retrofit.create(ApiService.class);
        return mInterfaceService;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_friend, container, false);
        bubblePicker = (BubblePicker) rootView.findViewById(R.id.picker);
        ApiService mApiService = getInterfaceService();
        Call < GetFriendsPhotoDao > mService = mApiService.photo("2", "2202", "2", "1", "1", "2558");
        //***** Call API Here *****
        mService.enqueue(new Callback < GetFriendsPhotoDao > () {
            @Override
            public void onResponse(Call < GetFriendsPhotoDao > call, Response < GetFriendsPhotoDao > response) {
                GetFriendsPhotoDao getFriendsPhotoDao = response.body();
                if (getFriendsPhotoDao.getSuccess()) {
                    students = getFriendsPhotoDao.getStudent();
                    studentsArray = new String[students.size()];
                    for (int i = 0; i < studentsArray.length; i++) {
                        studentsArray[i] = students.get(i).getStuId();
                    }
                } 
            }
            @Override
            public void onFailure(Call < GetFriendsPhotoDao > call, Throwable t) {
                call.cancel();
            }
        });
        addBubble();
        return rootView;
    }



//***** Add bubbles here *****
private void addBubble() {

    bubblePicker.setBubbleSize(10);
    bubblePicker.setAdapter(new BubblePickerAdapter() {
        @Override
        public int getTotalCount() {
            return studentsArray.length;
        }

        @NotNull
        @Override
        public PickerItem getItem(int position) {
            PickerItem item = new PickerItem(); item.setIcon(getResources().getDrawable(R.drawable.person));
            item.setTextColor(ContextCompat.getColor(getContext(), android.R.color.white));
            return item;
        }
    });
  }

...
}

3 个答案:

答案 0 :(得分:1)

一旦收到回复,您需要从addBubble();内移动onResponse

mService.enqueue(new Callback < GetFriendsPhotoDao > () {
            @Override
            public void onResponse(Call < GetFriendsPhotoDao > call, Response < GetFriendsPhotoDao > response) {
                GetFriendsPhotoDao getFriendsPhotoDao = response.body();
                if (getFriendsPhotoDao.getSuccess()) {
                    students = getFriendsPhotoDao.getStudent();
                    studentsArray = new String[students.size()];
                    for (int i = 0; i < studentsArray.length; i++) {
                        studentsArray[i] = students.get(i).getStuId();
                    }
                } 
             addBubble();
             //^^^^^^^
            }
            @Override
            public void onFailure(Call < GetFriendsPhotoDao > call, Throwable t) {
                call.cancel();
            }
        });
        //addBubble();

答案 1 :(得分:0)

实际上,您正在创建适配器的实例,并在Web服务覆盖onResponse方法之外设置适配器,这就是为什么数据未在适配器中设置的原因。试试这个 -

    public class FriendFragment extends Fragment {

    Http http = new Http();
    final String BASE_URL = http.getUrl();

    String[] studentsArray;
    List < Student > students;

    BubblePicker bubblePicker;


    public static FriendFragment newInstance() {
        FriendFragment fragment = new FriendFragment();
    return fragment;
    }

    private ApiService getInterfaceService() {
        Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
        final ApiService mInterfaceService = retrofit.create(ApiService.class);
        return mInterfaceService;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_friend, container, false);
        bubblePicker = (BubblePicker) rootView.findViewById(R.id.picker);
        ApiService mApiService = getInterfaceService();
        Call < GetFriendsPhotoDao > mService = mApiService.photo("2", "2202", "2", "1", "1", "2558");
        //***** Call API Here *****
        mService.enqueue(new Callback < GetFriendsPhotoDao > () {
            @Override
            public void onResponse(Call < GetFriendsPhotoDao > call, Response < GetFriendsPhotoDao > response) {
                GetFriendsPhotoDao getFriendsPhotoDao = response.body();
                if (getFriendsPhotoDao.getSuccess()) {
                    students = getFriendsPhotoDao.getStudent();
                    studentsArray = new String[students.size()];
                    for (int i = 0; i < studentsArray.length; i++) {
                        studentsArray[i] = students.get(i).getStuId();
                    }
                    addBubble();
                } 
            }
            @Override
            public void onFailure(Call < GetFriendsPhotoDao > call, Throwable t) {
                call.cancel();
            }
        });
        //addBubble();
        return rootView;
    }



//***** Add bubbles here *****
private void addBubble() {

    bubblePicker.setBubbleSize(10);
    bubblePicker.setAdapter(new BubblePickerAdapter() {
        @Override
        public int getTotalCount() {
            return studentsArray.length;
        }

        @NotNull
        @Override
        public PickerItem getItem(int position) {
            PickerItem item = new PickerItem(); item.setIcon(getResources().getDrawable(R.drawable.person));
            item.setTextColor(ContextCompat.getColor(getContext(), android.R.color.white));
            return item;
        }
    });
  }

...
}

答案 2 :(得分:0)

我认为您可以使用此方法:

@Override
protected void finalize() throws Throwable {
    super.finalize();
    addBubble();     //your func
}