Android:需要从主机名获取IP地址的帮助吗
我正在我的活动及其工作中使用以下工具。但是我得到的输出(主机名)不是我想要的方式。例如,如果我需要查找google的IP,我会像Android:需要从主机名获取IP地址的帮助吗,android,network-programming,Android,Network Programming,我正在我的活动及其工作中使用以下工具。但是我得到的输出(主机名)不是我想要的方式。例如,如果我需要查找google的IP,我会像www.google.com/74.125.224.72 它应该像74.125.224.72 我是Android新手,请帮帮我。这是我的密码: String host = editText1.getText().toString(); try { InetAddress[] hostInetAddress = InetAddress.getAllByName(h
www.google.com/74.125.224.72
它应该像74.125.224.72
我是Android新手,请帮帮我。这是我的密码:
String host = editText1.getText().toString();
try {
InetAddress[] hostInetAddress = InetAddress.getAllByName(host);
String all = "";
for(int i = 0; i < hostInetAddress.length; i++){
all = all + hostInetAddress[i].toString() + "\n";
}
textInetAddress.setText(all);
}
String host=editText1.getText().toString();
试一试{
InetAddress[]主机InetAddress=InetAddress.getAllByName(主机);
字符串全部=”;
for(int i=0;i
使用以下方法获取您的姓名/ip地址:
InetAddress address = InetAddress.getByName(new URL(urlString).getHost());
//it will fetch name/ip_address (eg. www.google.com/74.125.224.72) of the url you enter
然后使用以下命令仅获取ip_地址:
String ip = address.getHostAddress();
//it will fetch only ip_address (eg. "74.125.224.72" ) from the abouve url
所以不是
for(int i = 0; i < hostInetAddress.length; i++){
all = all + hostInetAddress[i].toString() + "\n";
}
for(int i=0;i
使用以下代码
for(int i = 0; i < hostInetAddress.length; i++){
all = all + hostInetAddress[i].getHostAddress().toString() + "\n";
}
for(int i=0;i
试试这个:
String[] ip = all.split("/");
textInetAddress.setText(ip[1]);
不需要for循环。这将起作用。
hostInetAddress[0]。getHostAddress() 用斜杠(/)分割值…如果所有地址都像你说的那样…我应该在
all
string中使用它吗?从哪里获取值?而不是`for(int i=0;itextInetAddress.setText(ip[1])为了解决这个问题,我需要设置变量CharSequence[]Thnx,这就解决了这个问题<代码>主机地址[i]。getHostAddress()