0

在我们只传递一个主机名的以下代码块中,您如何传递多个“主机名”?可能吗?

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();
  }
} 
4

2 回答 2

1

InetAddress没有采用 Strings 数组的方法。所以你不能那样做。

您可以创建自己的主机数组并使用 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();
    }  
}
于 2013-02-14T10:13:00.617 回答
0

我没有看到合适的 API,为什么不只是传递一个主机数组并循环它呢?

String[] hosts = {"www.google.com", "www.pippo.com", "...."};
for(String host : hosts){
  // Do your thing
}
于 2013-02-14T10:14:34.390 回答