【发布时间】:2016-05-28 15:37:56
【问题描述】:
我使用 writeObject 和 readObject 存储在存储中,以便在离线模式下也能工作 但是当没有网络连接时,它会给出无法访问的异常。大多数时候,它不 影响应用程序,但有时,应用程序不起作用。我必须关闭该应用程序,然后重新启动它 工作。
例外:
java.io.IOException: Unreachable
at com.codename1.impl.javase.JavaSEPort.connect(JavaSEPort.java:5355)
at com.codename1.impl.javase.JavaSEPort.connect(JavaSEPort.java:5387)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:299)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:263)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
代码:
void galleryCategoryOfflineStorage(Form f) {
Vector galleryVectorRead = (Vector) Storage.getInstance().readObject("galleryCategory");
connectionForGallery(f);
if (galleryVectorRead != null) {
galleryVector = galleryVectorRead;
}
if (galleryVector != null) {
for (int j = 0; j < galleryVector.size(); j++) {
Hashtable hm = (Hashtable) galleryVector.get(j);
String categoryId = (String) hm.get("category_id");
String categoryName = (String) hm.get("category_name");
String categoryIcon = (String) hm.get("image_url");
String name = categoryIcon.substring(categoryIcon.lastIndexOf("/") + 1, categoryIcon.lastIndexOf("."));
placeholder = placeholder.scaledEncoded((screenWidth / 2) - 5, (screenWidth / 2) - 5);
Image icon = URLImage.createToStorage(placeholder, "beckk" + name, categoryIcon, URLImage.RESIZE_SCALE_TO_FILL);
Container categoryContainer = new Container();
Button categoryButton = new Button();
categoryButton.setText(categoryName);
categoryButton.setTextPosition(Label.BOTTOM);
categoryButton.setUIID("categoryButton");
categoryButton.getAllStyles().setPadding(0, 0, 0, 0);
categoryButton.getAllStyles().setMargin(0, 0, 0, 0);
categoryButton.getAllStyles().setBgColor(0xcccccc);
categoryButton.getAllStyles().setBorder(Border.createBevelLowered());
categoryContainer.addComponent(categoryButton);
categoryButton.addActionListener((e) -> {
showGalleryImagesList(categoryId);
});
f.addComponent(j, categoryContainer);
f.setScrollableY(true);
f.revalidate();
}
}
}
我的连接:
public void connectionForGallery(Form f) {
galleryNoConnectionRequest = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
results = p.parse(new InputStreamReader(input));
galleryVector = (Vector<Map<String, Object>>) results.get("root");
// System.out.println("galleryVector " + galleryVector);
}
@Override
protected void postResponse() {
Storage.getInstance().writeObject("galleryCategory", galleryVector);
}
@Override
protected void handleErrorResponseCode(int code, String message) {
Dialog.show("Error msg", "The server returned the error code: " + code, "ok", null);
}
@Override
protected void handleException(Exception err) {
Dialog.show("Connection msg", "There was a connection error: " + err, "ok", null);
}
@Override
protected void handleIOException(IOException err) {
Dialog.show("IO exception", "There was a IO error: " + err, "ok", null);
}
};
galleryNoConnectionRequest.setPost(false);
galleryNoConnectionRequest.setUrl("http://myUrl");
galleryNoConnectionRequest.setDuplicateSupported(true);
InfiniteProgress ip = new InfiniteProgress();
Dialog dialog = ip.showInifiniteBlocking();
galleryNoConnectionRequest.setDisposeOnCompletion(dialog);
galleryNoConnectionRequest.setFailSilently(true);
NetworkManager.getInstance().addToQueueAndWait(galleryNoConnectionRequest);
}
此外,connectionRequest 类中还有handleIOException() 方法,但它不处理IOException: Unreachable 异常。
还有handleErrorResponseCode()方法,但是如果没有连接,它也不会响应。
【问题讨论】:
标签: java java-io codenameone ioexception