0

I'm sending data from android device to localhost database. To do this I need to use my ip address. I can find my ip address by searching 'ipconfig' on the command prompt. I just noticed that my ip address changed slightly even though im using the same wifi connection. The last digit of my ip address changed. This needed a minor change in my code but I was wonder if there was an android function that returned your computers ip address for you so that the code could look like below. Such a function would also help when using other forms of internet connection that would change your ip address.

HttpPost httpPost = new HttpPost("http://" + ipAddressFunction() + "/linker.php");// home wifi 
will
  • 75
  • 1
  • 7
  • Is this any help? http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device – TayTay Mar 24 '15 at 18:36
  • 1
    There's no direct way for your Android phone the know the address of another machine on the network, without either using some name service or some sort of broadcast service discovery scheme, or being informed of it by some other mechanism. – Chris Stratton Mar 24 '15 at 18:43
  • @ChrisStratton Are there any examples on stackoverflow? – will Mar 24 '15 at 18:51
  • Yes, all three are likely covered. Pick one and research it. – Chris Stratton Mar 24 '15 at 18:59
  • Switch off the DHCP protocol on your PC and give it a fixed local ip address. – greenapps Mar 24 '15 at 20:07

3 Answers3

0

first get your network-interfaces via:

NetworkInterface.getNetworkInterfaces();

then get the IP(s) va:

getInetAddresses()
ligi
  • 39,001
  • 44
  • 144
  • 244
  • As I read the question, they want one machine (the Android device) to determine the IP address of a *different* machine (some box on the local wifi hosting a server - likely a windows one given the `ipconfig` command). – Chris Stratton Mar 24 '15 at 18:42
0

Here is a function i made to get my Android Device IP.

//Get The Local IP Addresses
private String GetLocalIPAddress(Boolean LoopBack)
{
    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.getHostAddress().contains("."))
                {
                    if(inetAddress.isLoopbackAddress() == LoopBack)
                    {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        Log.d(this.getClass().toString(), "Failed Getting Local IP\n\n" + e.getMessage());
    }
    return null;
}

This function receives a Boolean parameter, if True, it will return the Local Loopback IP (I.E: 127.0.0.1), if False, it will return the normal IP (I.E: 192.168.0.5)

Ivan Verges
  • 595
  • 3
  • 10
  • 25
  • this does return android ip address but i need the one for my computer – will Mar 24 '15 at 18:52
  • Well, i have no idea about how to get an specific PC IP from a Mobile Device. But if only the last digits do change, you can set a scanning loop giving a range of options. – Ivan Verges Mar 24 '15 at 19:28
-1

You can try to connect multiple IP, if have the return data is proved to be a computer IP

oldfeel
  • 420
  • 3
  • 12