2012-12-02 136 views
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解析失敗 在好日子指定默認值

+1

問題你不清楚你要做什麼嗎? –

+1

並請形成適當的問題標題。 –

+2

你在做什麼樣的工作呢? – PermGenError

回答

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

這個例子語境中不應WIFI/3G連接disponible,謝謝你們指定的inputline的價值是java的7多重捕獲語法,請介意。

1

-如果的WiFi/3G失敗,則它會給UnknownHostException

所以,你可以處理他們這樣....

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

     } catch (UnknownHostException e) { 

      inputLine = "Connection error"; 
    }