Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:需要从主机名获取IP地址的帮助吗_Android_Network Programming - Fatal编程技术网

Android:需要从主机名获取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

我正在我的活动及其工作中使用以下工具。但是我得到的输出(主机名)不是我想要的方式。例如,如果我需要查找google的IP,我会像
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])主机地址[i]。getHostAddress()