这是我的代码:
private void getUserData(){
//create Retrofit instance
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
Retrofit retrofit = builder.build();
//get client & call object for the request
APIService userService = retrofit.create(APIService.class);
Map<String, Object> map = new HashMap<>();
map.put("device_id", Utils.GetDeviceID(DashboardActivity.this));
Call call = userService.getUser(map);
//execute network request
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful() && response.body() != null){
response.body();
UserDataManager.$().setUserDataResponse(new com.google.gson.Gson().toJson(response.body()));
if(UserDataManager.$().getUserData() != null){
Log.d(TAG, "USER ID: " + UserDataManager.$().getUserData().getId());
}
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
问题:如何每5秒调用此方法,以检查后端是否有新数据到来?
如果有建议和提示,我会很高兴,谢谢。