How to programmatically get a public IP address?

python get public ip address
my ip address
javascript get public ip
android get ip address of mobile network programmatically
android get public ip address programmatically
android get ip address programmatically
android get ip address ipv4
get user ip address android

I didn't find the right solution. The below code gives me local IP address (if I connected to Wifi, it gives IP address like 192.168.0.x), but I want public IP address (same as if I search in google " what is my IP ")

public static 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() && inetAddress instanceof Inet4Address) {
                return inetAddress.getHostAddress();
} catch (SocketException ex) {
return null;


WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Can anyone help? Thanks!

Step #1: Create a Web service that returns the requester's IP address

Step #2: Call that Web service from your app.

A device does not know its public IP address (unless that device was seriously misconfigured).

ipify, ipify API is a simple public IP address API, easy enough to integrate into any If you need to get the geolocation data by IP, please refer to our. IP Geolocation API. Why ipify? Ever needed to get your public IP address programmatically? We are able to get Local Ip Address with WifiManager or InetAddress. But we need the Current Public IP address programmatically in android.

How to get the IP address of android device programmatically?, This example demonstrates how do I get the IP address of android device TextView; public class MainActivity extends AppCompatActivity  @Mathieu, and others who ask why the address given by this method is different to what you get from etc. - That's because this method gives your LOCAL IP address. That is, the IP which other devices on your local area network (home hub, WiFi network, office intranet etc.) will use to talk to your device.

I found this simple solution:

public String getExternalIpAddress() throws Exception {
    URL whatismyip = new URL("");
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
        String ip = in.readLine();
        return ip;
    } finally {
        if (in != null) {
            try {
            } catch (IOException e) {

Remember that this must be run on a separate thread.

How To Determine Your Public IP Address Programmatically , How To Determine Your Public IP Address Programmatically hosts: localhost vars: tasks: - name: Get my public IP ipify_facts: timeout: 20  The short video below demonstrates two “manual” ways of retrieving the local IP and MAC address of your computer, as well as a single way to retrieve your public/external IP: Network connection details (local IP and MAC address). Command prompt – ipconfig and getmac (local IP and MAC address). Internet (public IP).

You can do this with a simple thread. you need to create a function in Activity.class file, and need to request a url that will give your public IP in text form: " Click to open.

Add this function call in your onCreate() function.


Add this function in your MainActivity.class.

    private void getPublicIP() {

        new Thread(new Runnable(){
            public void run(){
                //TextView t; //to show the result, please declare and find it inside onCreate()

                try {
                    // Create a URL for the desired page
                    URL url = new URL(""); //My text file location
                    //First open the connection
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(60000); // timing out in a minute

                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    //t=(TextView)findViewById(; // ideally do this in onCreate()
                    String str;
                    while ((str = in.readLine()) != null) {
                } catch (Exception e) {

                //since we are in background thread, to post results we have to go back to ui thread. do the following for that

                PermissionsActivity.this.runOnUiThread(new Runnable(){
                    public void run(){
                        try {
                            Toast.makeText(PermissionsActivity.this, "Public IP:"+urls.get(0), Toast.LENGTH_SHORT).show();
                        catch (Exception e){
                            Toast.makeText(PermissionsActivity.this, "TurnOn wiffi to get public ip", Toast.LENGTH_SHORT).show();



Android get public ip address programmatically android, Android get public ip address programmatically android. In Android there is no option to manually add server address like it is there in windows time setting. Why ipify? Ever needed to get your public IP address programmatically? Maybe you're provisioning new cloud servers and need to know your IP -- maybe you're behind a corporate firewall and need to tunnel information -- whatever the reason: sometimes having a public IP address API is useful! You should use ipify because:

Make a call to a server like or

If you have the page source then parse the ip address out.

There are other servers who only return your ip address. Not a whole html page as above two. But i forgot which one...

What Is My Public IP Address, How do I find the IP address in android programmatically? However, getting your public IP address programmatically may not be as easy. The actual IP address value is normally buried in a bunch of HTML code. This, then, requires scraping. Not fun. Well, here’s an easy way to get your public IP address using PowerShell… How do I get my public IP address Using PowerShell?

Status of, is being used by malware C&C. As of the date of this post, the site itself is not currently malicious, instead it is being abused by malicious software. return address; In the above code snippet, we are making a request to the website that displays the IP address of the current computer. We are receiving the response and parsing the response to get the IP address.

Rest - how get IP address of caller, How do I find my IP address on REST API? Depending on the exact implementation, there could be ways to retrieve the public address. For example, presuming that the PC is directly connected to the internet (no NAT device / router) AND the PC is running the VPN software, that information c

Android get public ip address programmatically android, If the MAC filt Android Examples Tutorials; « Get/Display IP address of android phone device programmatically. Change public IP address on a WiFi network. How to programmatically get the IP address during a Terraform run and put in to an application config file? Hi! I am currently using Terraform + Ansible to build out my infrastructure.

  • Possible duplicate of How to get IP address of the device from code?
  • The other answers were driving me nuts. Thanks a ton!
  • Use WS to get device public IP address That does not work. DId you try before posting?
  • You must be a Gold Level Member to access the API. Are you a Gold Level Member of
  • It does not make much sense to use a server where you need to be a member. And why didnt you tell right away?
  • They are there decades. Like the one you mentioned. No difference.
  • Make sure that your privacy policy points out that you are making these requests of somebody else's Web service, rather than one of your own.
  • Awesome solution available online :)
  • Is there any max limit for using this api?
  • Make sure that your privacy policy points out that you are making these requests of somebody else's Web service, rather than one of your own.