我遇到了连接错误时应用程序行为方式的问题。
当用户填写用户/密码字段时,我执行以下操作:
public static AuthenticationResponse authenticate (String username, String password){
return WebServiceCaller.authenticate(username, password, ServiceDefinitions.getAuthenticationServiceDefintion());
}
在网络服务调用者中我有:
public static AuthenticationResponse authenticate(String username, String password, ServiceDefinition definition) {
AuthenticationService authenticationService = new AuthenticationService(username, password);
NetworkManager.getInstance().setTimeout(10000); // change the default timeout of network.
service.setTimeout(10000); // change the default timeout of request connection
service.setUrl(definition.getUrl());
service.setContentType(definition.getContentType());
service.setPost(definition.isPost());
InfiniteProgress ip = new InfiniteProgress();
Dialog dlg = ip.showInifiniteBlocking();
service.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueueAndWait(authenticationService);
return authenticationService.getModelResponse();
}
我还重写了 handleIoException
和 handleException
方法,因此我没有显示通用对话框,而是用一条消息填充响应对象,并且该消息显示在 GUI 中。
发生的错误事情是在错误管理方法完成之前调用上述方法的返回部分。
所以基本上应该是 call->wait->errorManagement
complete->return
但我得到的是 call->wait->errorManagement->return->errorManagemetnComplete
。
也许我遗漏了网络管理器的工作方式及其多线程操作的某些内容,如果您有任何更详细的文档,我很乐意阅读。
我在重写方法中所做的事情如下所示:
/** Handles IOException thrown when performing a network operation */
@Override
protected void handleIOException(IOException err) {
Log.p("Handle IOException, when network is down! ");
manageGenericErrors();
}
@Override
public void manageGenericErrors() {
response.setIsSuccessful(false);
response.setResponseMessage("Service not available”);
}
最佳答案
错误处理首先到NetworkManager
错误监听器,然后才调用句柄方法(例如 handleException
)。
这很重要,因为您可能希望以不同的方式全局处理错误,例如如果您有如下代码:
NetworkManager.getInstance().addErrorListener((e) -> e.consume());
在此代码中,您将以通用方式有效阻止 Codename One 中的所有错误处理,并且这必须在调用 handle* 方法之前发生,因为它们显示错误对话框。
addToQueueAndWait
方法通过监听网络/错误事件来阻止。在正常完成时,这工作得很好,但在发生错误事件时,您将在调用句柄方法之前获得错误监听器回调。自通用addToQueueAndWait
方法不能覆盖方法,并且如果您定义不希望显示错误,则应该仍然可以正常工作,这是该 API 基本设计的核心。
我没有看到一个通用的方法来让它以不同的方式工作。解决方法是使用 NetworkManager
的addErrorListener()
而不是重写这些方法。
关于java - Codename One ConnectionRequest 中出现连接错误时的不正确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390805/