Fetch ip address of pc on android emulator through code android

不羁岁月 提交于 2019-12-08 01:19:48

问题


I want to get the ip address of my pc in android emulator through code....or tell me to achive ip address of all devices connected in a lan to identify each one uniquely .......please help me to sort out this problem

Thanks in advance


回答1:


The above functions are possible only by checking the arp cache where the IP address will be added one by one depending on how each one connect to the device. USe the below code and check. Just put button with proper name and call this method on click

    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();
        }
    }
}



回答2:


use this code fetch the external ip address

      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();
    }
    } 
      }


来源:https://stackoverflow.com/questions/10136082/fetch-ip-address-of-pc-on-android-emulator-through-code-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!