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接続が無責任ではないコンテキストでは、皆さん、ありがとうございました

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 に答える