首页 > 解决方案 > 在 Android Studio 中 Ping 本地 IP

问题描述

我正在尝试开发一种方法来检查 IP 方向是否可达,我发现了一个类似的问题,但它不起作用 Android Application Ping IP 号码

private void existePingServidor(){
    InetAddress in;
       try{
            in = InetAddress.getByName("90.0.0.122");

       if (in.isReachable(5000)){
           pingServidor = true;
           Log.v("true","He pasado por aquí");
       }
       else
           pingServidor = false;
       Log.v("false", "No he podido alcanzar la ip");

    }

    catch(Exception  e){
        e.printStackTrace();
    }
}

问题似乎是 InetAddress.getByName("90.0.0.122"); 返回空

¿你能解释一下在android studio中ping一个IP的正确方法是什么吗?

标签: androidping

解决方案


尝试 getByAddress 代替:

    byte [] ip = {90, 0, 0, 122};
    addr = InetAddress.getByAddress(ip);
    addr.isReachable(5000);

推荐阅读