IOException:无法访问的异​​常codenameone

时间:2016-02-17 04:46:33

标签: java java-io codenameone ioexception

我使用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();
            }
        }
    }

MyConnection的:

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);
    }

此外,handleIOException()类中有connectionRequest方法,但它不会处理IOException: Unreachable例外。

此外还有handleErrorResponseCode()方法,但如果没有连接,它也不会响应。

2 个答案:

答案 0 :(得分:0)

我希望 galleryNoConnectionRequest.settimeout(30000); 会工作的。

答案 1 :(得分:-1)

如果您的网络存在问题,则无法访问主机。从您的其他问题我了解到这是您遇到的一般网络问题。

如果您的网络不可靠,您需要捕获异常/错误并正确处理它们。