2013-02-14 62 views
0

如何在傳遞一個主機名的以下代碼塊中傳遞多個「主機名」?可能嗎?如何在「InetAddress」方法中傳遞「主機名」數組而不是傳遞單個主機名

private static void run() { 

    String host = "www.google.com"; 
    try { 
    inetAddress = InetAddress.getAllByName(host); 
    String all = ""; 
    for (int i = 0; i < inetAddress.length; i++) { 
     all = all + String.valueOf(i) + " : " + inetAddress[i].toString() + "\n"; 
     Log.d("IPADDR", "IP Address : " + all);     
     prefs.sethostIPaddress(context, all); //Setting HostIP Address in Preference File 
    } 
    } 
    catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 
} 

回答

1

InetAddress沒有采用字符串數組的方法。所以你不能那樣做。

您可以創建自己的主機陣列並使用for循環獲取InetAddress。像

String [] hosts = {"host1", "host2", "host3"}; 

for(String host : hosts){ 
    try { 
     inetAddress = InetAddress.getAllByName(host); 
     String all = ""; 
     for (int i = 0; i < inetAddress.length; i++) { 
      all = all + String.valueOf(i) + " : " + inetAddress[i].toString() + "\n"; 
      Log.d("IPADDR", "IP Address : " + all); 
      prefs.sethostIPaddress(context, all); //Setting HostIP Address in Preference File  
     } 
    } 
    catch (UnknownHostException e) { 
      e.printStackTrace(); 
    } 
} 
+0

三江源傢伙,薩米特和Wizche ...它的工作:d乾杯 – 2013-02-14 10:54:42

+0

您的歡迎:) – 2013-02-14 11:48:43

0

我沒有看到一個適當的API,爲什麼不只是傳遞一個主機數組並循環它呢?

String[] hosts = {"www.google.com", "www.pippo.com", "...."}; 
for(String host : hosts){ 
    // Do your thing 
} 
+0

三江源傢伙,薩米特和Wizche ...它的工作:d乾杯 – 2013-02-14 10:54:23