如果我试图阅读空白网页,我将如何返回默认字符串?

private void setVersion(String input) {
        try {
            URL url = new URL(input);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            String getInfo;
            while ((getInfo = in.readLine()) != null) {
                Console("[setVersion Length]" + getInfo.length());
                dModVersionList.addElement(getInfo);
            }

        } catch (Exception e) {
            dModVersionList.addElement("Unknown");
            e.printStackTrace();
        }
    }


即使while循环返回null时,我也想添加元素,如果页面不再存在,我已经可以处理它

最佳答案

这样的事情可以工作:

        String getInfo;
        // set initially to false
        boolean found = false;
        while ((getInfo = in.readLine()) != null) {
            found = true; // found some data; set to true
            Console("[setVersion Length]" + getInfo.length());
            dModVersionList.addElement(getInfo);
        }
        // still false; add your specific element
        if (!found)
            dModVersionList.addElement("no data");

10-07 16:09