0
        URL test = null;
        String inputLine = null;
        BufferedReader in = null;
        try {
            test = new URL("http://localhost/out.php"); // if connection down
        } catch (MalformedURLException e) {
            inputLine = "test_synntax";
        }
        try {
        in = new BufferedReader(new InputStreamReader(test.openStream()));
        ...

如果默认情况下 URL 解析失败,如何为 inputline 分配一个值在示例上下文中不是 wifi/3g 连接 disponible,谢谢大家好日子

4

3 回答 3

1
try {
  test = new URL("http://localhost/out.php");
  //I believe you need this and it throws IOException on timeout. 
  //Though it's still unclear to me what you mean by 'resolution' and 'disponible wifi/3g`?
  test.openConnection(); 
} catch (MalformedURLException | IOException e) { 
  inputLine = "test_synntax";
}

请注意,这是 java 的 7 种多捕获语法。

于 2012-12-02T14:11:32.540 回答
1

-如果wifi/3g出现故障,它会给出UnknownHostException.

所以你可以这样处理它们......

try {
            test = new URL("http://localhost/out.php");

        } catch (UnknownHostException e) {

            inputLine = "Connection error";
   }
于 2012-12-02T14:11:42.237 回答
1

您需要先调用URL.openConnection,然后将默认值放入IOException块中:

try {
   test = new URL("http://localhost/out.php");
   URLConnection urlConn = test.openConnection();
   in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
} catch (MalformedURLException e) {
    inputLine = "test_synntax";
} catch (IOException e) {
    inputLine = "test_synntax";
}
于 2012-12-02T14:15:12.857 回答