通过代码安卓在安卓模拟器上获取电脑的IP地址



我想通过代码在安卓模拟器中获取我的电脑的 IP 地址......或者告诉我获得局域网中连接的所有设备的IP地址,以唯一地识别每个设备.......请帮我解决这个问题

提前致谢

上述功能只能通过检查 arp 缓存来实现,其中 IP 地址将根据每个缓存连接到设备的方式逐个添加。使用以下代码并检查。只需使用正确名称放置按钮并在单击时调用此方法

    public void getClientList() {
    int macCount = 0;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    macCount++;
                    ClientList.add("Client(" + macCount + ")");
                    IpAddr.add(splitted[0]);
                    HWAddr.add(splitted[3]);
                    Device.add(splitted[5]);
                    Toast.makeText(
                            getApplicationContext(),
                            "Mac_Count  " + macCount + "   MAC_ADDRESS  "
                                    + mac, Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < splitted.length; i++)
                        System.out.println("Addressssssss     "
                                + splitted[i]);
                }
            }
        }
        // ClientList.remove(0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用此代码获取外部 IP 地址

      HttpClient httpclient = new DefaultHttpClient();
      HttpGet httpget = new HttpGet("http://api.externalip.net/ip/"); 
      HttpResponse response = null;
      try 
      {
      response = httpclient.execute(httpget);
        } 
      catch (ClientProtocolException e)
      {
     e.printStackTrace();
        } 
      catch (IOException e)
      {
     e.printStackTrace();
        }
      Log.e("",""+response);
      HttpEntity entity = response.getEntity();
      if (entity != null) {
      long len = entity.getContentLength();
      if (len != -1 && len < 1024) 
      {
       try
       {
      str=EntityUtils.toString(entity);
       Log.e("",""+str);
        }
       catch (ParseException e)
       {            
    e.printStackTrace();
    } 
       catch (IOException e)
       {                
    e.printStackTrace();
    }
    } 
      }

最新更新