如何等到Callback有数据?
How to wait until Callback has data?
我正在尝试使用 Retrofit 和 OkHttp 向服务器发出请求。我有下一个 class "AutomaticRequest.java",它请求从服务器获取视频。
public class AutomaticRequest {
public void getVideos(final AutomaticCallback callback){
MediaproApiInterface service = ApiClient.getClient();
Call<List<AutomaticVideo>> call = service.getAllVideos();
call.enqueue(new Callback<List<AutomaticVideo>>() {
@Override
public void onResponse(Call<List<AutomaticVideo>> call, Response<List<AutomaticVideo>> response) {
List<AutomaticVideo> automaticVideoList = response.body();
callback.onSuccessGettingVideos(automaticVideoList);
}
@Override
public void onFailure(Call<List<AutomaticVideo>> call, Throwable t) {
callback.onError();
}
});
}
}
我创建了下一个 class "AutomaticCallback.java" 来检索数据。
public interface AutomaticCallback {
void onSuccessGettingVideos(List<AutomaticVideo> automaticVideoList);
void onError();
}
我正在从一个片段调用请求,如下一种方式:
public class AllVideosFragment extends Fragment {
...
AutomaticCallback callback;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_allvideos, container, false);
new AutomaticRequest().getVideos(callback);
return view;
}
...
}
我如何才能等到回调有数据更新 UI?谢谢。
只需在您的片段上实现 AutomaticCallback 接口,例如:
public class AllVideosFragment extends Fragment implements AutomaticCallback {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_allvideos, container, false);
new AutomaticRequest().getVideos(this);
return view;
}
@Override
void onSuccessGettingVideos(List<AutomaticVideo> automaticVideoList){
// use your data here
}
@Override
void onError(){
// handle the error
}
}
我正在尝试使用 Retrofit 和 OkHttp 向服务器发出请求。我有下一个 class "AutomaticRequest.java",它请求从服务器获取视频。
public class AutomaticRequest {
public void getVideos(final AutomaticCallback callback){
MediaproApiInterface service = ApiClient.getClient();
Call<List<AutomaticVideo>> call = service.getAllVideos();
call.enqueue(new Callback<List<AutomaticVideo>>() {
@Override
public void onResponse(Call<List<AutomaticVideo>> call, Response<List<AutomaticVideo>> response) {
List<AutomaticVideo> automaticVideoList = response.body();
callback.onSuccessGettingVideos(automaticVideoList);
}
@Override
public void onFailure(Call<List<AutomaticVideo>> call, Throwable t) {
callback.onError();
}
});
}
}
我创建了下一个 class "AutomaticCallback.java" 来检索数据。
public interface AutomaticCallback {
void onSuccessGettingVideos(List<AutomaticVideo> automaticVideoList);
void onError();
}
我正在从一个片段调用请求,如下一种方式:
public class AllVideosFragment extends Fragment {
...
AutomaticCallback callback;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_allvideos, container, false);
new AutomaticRequest().getVideos(callback);
return view;
}
...
}
我如何才能等到回调有数据更新 UI?谢谢。
只需在您的片段上实现 AutomaticCallback 接口,例如:
public class AllVideosFragment extends Fragment implements AutomaticCallback {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_allvideos, container, false);
new AutomaticRequest().getVideos(this);
return view;
}
@Override
void onSuccessGettingVideos(List<AutomaticVideo> automaticVideoList){
// use your data here
}
@Override
void onError(){
// handle the error
}
}