0

次のコードのチャンクで複数の「ホスト名」を渡すには、ホスト名を 1 つだけ渡しますか? 出来ますか?

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

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