[英]Retrofit with Vaadin UI
I am developing a project with Vaadin where he has to make API calls to some services I have created.我正在与 Vaadin 开发一个项目,他必须在其中对我创建的某些服务进行 API 调用。 To make API calls use retrofit.要进行 API 调用,请使用改造。 The problem I'm having is that once I get the (asynchronous) response in the onResponse function override I can't change the UI anymore, otherwise I get the error: "Cannot access state in VaadinSession or UI without locking the session."我遇到的问题是,一旦我在 onResponse 函数覆盖中获得(异步)响应,我就无法再更改 UI,否则我会收到错误消息:“无法在不锁定会话的情况下访问 VaadinSession 或 UI 中的状态。”
service.getUsers().enqueue(new Callback<>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
if (response.isSuccessful()) {
assert response.body() != null;
// Receives data correctly
System.out.println("Response: " + response.body());
// set and add UI element (Error)
setUI(response.body());
} else {
System.out.println("TEST > " + response.errorBody());
}
}
@Override
public void onFailure(Call<List<User>> call, Throwable throwable) {
System.out.println("onFailure: " + throwable.getMessage());
}
});
You have to update the UI in the access() method:您必须在 access() 方法中更新 UI:
For example:例如:
getUI().ifPresent(ui -> ui.access(() -> {
// update the components
}
Please also read the documentation: https://vaadin.com/docs/v14/flow/advanced/tutorial-push-access另请阅读文档: https : //vaadin.com/docs/v14/flow/advanced/tutorial-push-access
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.