2013-01-05 71 views
3

所以我創建了一個小方法來請求發送到一個網站,我想,以確保它沒有崩潰。當我斷開互聯網並打電話給該方法時,我收到了一個java.net.UnknownHostException:,但我無法抓住它。我究竟做錯了什麼?IOException異常沒抓的java.net.UnknownHostException:

那是給問題的行ATM是HttpResponse response = httpclient.execute(httppost);

我甚至處理IO前ClientProtocolException(IO的子類),這樣我就可以有兩個catch塊。我也嘗試加入throws clauses作爲高層次Exception e。這裏是我的代碼:

public String connect(String username, String password) { 
    String answer = "Could not connect to server, please check your internet connect or try later."; 
    try { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(baseurl); 
     // Request parameters and other properties. 
     List<NameValuePair> params = new ArrayList<NameValuePair>(2); 
     params.add(new BasicNameValuePair("usernameUS", username)); 
     params.add(new BasicNameValuePair("passwordUS", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); 

     // Execute and get the response. 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 

     if (entity != null) { 
      answer = EntityUtils.toString(entity, charset); 
      try { 
       if (answer.equals("Authenticated")) { 
        this.password = password; 
        this.username = username; 
        Authenticated = true; 
        return answer; 
       } else { 
        return answer; 
       } 
      } finally { 
      } 
     } 
     return "Could not connect to server, please try later."; 
    } catch (ClientProtocolException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, 
       null, ex); 
     return answer; 
    } catch (IOException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, 
       null, ex); 
     return answer; 
    } 
} 

和異常:

java.net.UnknownHostException: www.example.com 
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) 
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:866) 
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1258) 
    at java.net.InetAddress.getAllByName0(InetAddress.java:1211) 
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) 
    at java.net.InetAddress.getAllByName(InetAddress.java:1063) 
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) 
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278) 
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162) 
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) 
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640) 
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) 
    at usclient.Connection.connect(Connection.java:65) 

工作代碼:

public class Connection { 
private static Connection conn; 
String username, fileURL; 
String baseurl = "http://www.example.com"; 
String content, password = ""; 
String charset = "UTF-8"; 
HttpURLConnection request; 
URL url; 
OutputStreamWriter post; 
BufferedReader in; 

private Connection() { 
} 

public static Connection getInstance() { 
    if(conn==null) { 
     conn = new Connection(); 
    } 
    return conn; 
} 

public String connect(String username, String password){ 
    String answer = "Could not connect to server, please check your internet connect or try later."; 
    try { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(baseurl); 
     // Request parameters and other properties. 
     List<NameValuePair> params = new ArrayList<NameValuePair>(2); 
     params.add(new BasicNameValuePair("usernameUS", username)); 
     params.add(new BasicNameValuePair("passwordUS", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); 

     //Execute and get the response. 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      String[] data = EntityUtils.toString(entity, charset).split("\\n"); 
      answer = data[0]; 
      try { 
       if(answer.contentEquals("Authenticated")) { 
        this.password = password; 
        this.username = username; 
        if(data.length<1) 
         fileURL = data[1]; 
        return data[0]; 
       } 
       else { 
        return answer; 
       } 
      } finally { 
      } 
     } 
     return "Could not connect to server, please try later."; 
    } catch (ClientProtocolException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, "Exception caught:", ex); 
     return empty; 
    } catch (IOException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, "Exception caught:", ex); 
     return empty; 
    } 
} 
+1

該線把它扔? – SJuan76

+0

也會加上問題,對不起。 HttpResponse response = httpclient.execute(httppost); – Juan

+7

你確定這個堆棧跟蹤沒有被catch塊中的Logger.log()調用打印出來嗎? –

回答

0

我敢肯定,這個問題應該被關閉。爲什麼?

我只是檢查了源,你在問題已經提供。

Connection類的最後一個版本,甚至並不希望編譯。 Java抱怨陳述return empty;。什麼是這個變量empty

好吧,即使改變return empty;return answer;(例如) - 一切正常。

例如,如果我嘗試連接到http://www.fdsfsaddsfsddsafasd.com,則java.net.UnknownHostException被成功捕獲到最後的...catch(IOException ...聲明中。

我建議你完全重建你的項目,在調試模式下運行它,並再次做了測試。