【发布时间】:2016-09-16 13:24:02
【问题描述】:
1)使用actionListener作为参数的ConnectionRequest
ArrayList<Map<String, Object>> responses;
public void groupConnection(StateMachine sm, ActionListener al) {
ConnectionRequest connectionRequest = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jSONParser = new JSONParser();
Map<String, Object> parsedData = jSONParser.parseJSON(new InputStreamReader(input));
responses = (ArrayList<Map<String, Object>>) parsedData.get("root");
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
al.actionPerformed(null);
}
});
}
@Override
protected void handleException(Exception err) {
//System.out.println("handleException " + err);
}
@Override
protected void handleIOException(IOException err) {
//toastbar doesnt work here but dialogBox works, if showForm("Groups") is used, toastbar is also shown along with dialogbox
//ToastBar.showErrorMessage("Please check your network connection", 4000);
//sm.showForm("Groups", null);
Dialog.show("", "Please check your network connection", "ok", null);
}
};
AllUrl allUrl = new AllUrl();
connectionRequest.setUrl(allUrl.groupsMenu);
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
connectionRequest.setDisposeOnCompletion(d);
NetworkManager.getInstance().addToQueue(connectionRequest);
}
在上面的 connectionRequest 代码中,如果没有网络连接,它会给出 IoException: unreachable ,由下面的 handleIOException 方法处理,但如果我在那里使用 dialogBox,它可以工作。相反 toastbar 在那里不起作用,这是为什么呢?如果我将 showForm("Form",null) 与 dialogBox 和 toastbar 一起使用,则会重复调用同一个表单,并且还会看到 toastbar 并多次出现对话框。
2)postForm(Form f)方法
connectionGroup = new GroupConnection();
connectionGroup.groupConnection(StateMachine.this, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//checking connectionGroup.responses == null doesnt work since the connectionRequest gives IOException if no network.So Toastbar doesnt work here.
if (connectionGroup.responses == null) {
ToastBar.showErrorMessage("Please check your network connection", 4000);
}
if (connectionGroup.responses != null) {
for (Map<String, Object> element : connectionGroup.responses) {
String tableName = (String) element.get("name");
TextArea nameLabel = new TextArea(tableName.toUpperCase());
f.add(singleRowContainerr);
}
}
}
}
在上面的 postForm 方法中,我曾经通过检查来检查是否有网络连接: if (connectionGroup.responses == null) { do smth...},它在这里不起作用,因为 connectionRequest 给出了 IoException 并且没有在 connectionRequest 中运行代码。
如何解决这个问题?我必须在 toastBar 中显示“检查连接”以及带有文本“无连接”的标签。 PS 我需要将我所有的组件和 connectionRequest 放在 postForm 中,因为我需要在调用 connectionRequest 之前转到表单。
【问题讨论】:
标签: codenameone