【问题标题】:IOException: Unreachable exception codenameoneIOException:无法访问的异​​常代号
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 为什么ConnectionRequest类的handleIOException()、handleErrorResponseCode()、handleException()方法处理不了?如果不是,我该如何分别处理它们?实际上我的另一个问题与此无关。那是 webBrowser 的问题,只有在没有网络连接问题时才会出现此异常。
      • 简单,您看到的异常是针对不同的连接。不适用于静默连接请求失败。例如。你是在下载图片还是什么的?
      • 不,我没有下载,jst一个urlImage缓存如上连接
      • URLImage 也会默默地失败。打开NetworkMonitor,查看触发这个的具体URL请求。
      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多