gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:03:18 25 4
gpt4 key购买 nike

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

我想是因为 enqueue() 是异步方法,所以它会在后面的行中执行而不会等待。我不知道如何解决这个问题。

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

请多多指教。谢谢

我的代码:

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;
}
});
}

...
}

最佳答案

一旦收到响应,您需要从 onResponse 内部移动 addBubble();

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();

关于android - 完成 Retrofit 调用后如何调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47778618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com