0

How to get the real IP address ? I use Code below, the result always be 127.0.0.1

if (getIpType(context) == IP_TYPE_WIFI) {

    WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifi_service.getDhcpInfo();
    WifiInfo wifiinfo = wifi_service.getConnectionInfo();
    String ip = Formatter.formatIpAddress(dhcpInfo.ipAddress);

} else {

    Runnable IpRunnable = new Runnable() {

        @Override
        public void run() {

            InetAddress addr;
            String localIp = null;

            try {
                addr = InetAddress.getLocalHost();
                localIp = addr.getHostAddress();
            } catch (UnknownHostException e) {
            }
        }
    };

    Thread payThread = new Thread(IpRunnable);
    payThread.start();
}
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Zhang Ligao
  • 147
  • 1
  • 2
  • 10
  • https://stackoverflow.com/questions/6305918/code-to-detect-the-android-devices-own-ip-address?rq=1 – CommonsWare Sep 05 '16 at 17:56
  • Note that "the real ip address" is a concept not so unique. If your phone is attached to some wifi network (e.g. your home wifi), it will have an IP address local to your home network, but will present itself to the outside word with the IP address assigned to the home router. To test the problem with a Windows PC try with an `ipconfig` from the command line compared to browsing https://www.whatsmyip.org/. I dont' know how to execute the same test with android, but surely there is a way to do it – Gian Paolo Jul 08 '21 at 19:29

3 Answers3

0

Try this

public String getLocalIpAddress(){
   try {
       for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();  
       en.hasMoreElements();) {
       NetworkInterface intf = en.nextElement();
           for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
           InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                return inetAddress.getHostAddress().toString();
                }
           }
       }
       } catch (Exception ex) {
          Log.e("IP Address", ex.toString());
      }
      return null;
}

source https://stackoverflow.com/a/11432695/5284441

Community
  • 1
  • 1
Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
0

you cant get the public ip address through the api android applied. the method is send a request to some website which will response infomations about your public ip address, and then parse the result to get the public ip.

Zhang Ligao
  • 147
  • 1
  • 2
  • 10
-1

Simply use Volley to get the ip from this site

 RequestQueue queue = Volley.newRequestQueue(this);

    String urlip = "http://checkip.amazonaws.com/";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, urlip, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            txtIP.setText(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            txtIP.setText("didnt work");
        }
    });

    queue.add(stringRequest);
Sohel Mahmud
  • 187
  • 12