title: Get host ip android
body: <p>Tried to do my homework, but can't figure it out.</p>
  
  <p>I am making an app that will run on a phone that stays connected to my home wifi 24/7.  I want it to detect the host ip and track any changes, but I can't get the code to work.  I found the InetAddress class, that seems like what I need, but the following code throws a generic exception "something bad is going wrong", I don't know how to pin it down, since its not the UnknownHostException that the method throws in the documentation:</p>
  
  <pre><code>public void getIP(View view) {
      isNetOK();
      getLocalIpAddress();
  
  }
  
  public void isNetOK() {
      //get connectivity manager
              connMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
              //call getActiveNetworkInfo method of connMan, returns a networkInfo object
              NetworkInfo netInfo = connMan.getActiveNetworkInfo();
              if (netInfo !=null &amp;&amp; netInfo.isConnected()) {
                  setViewText("net_stat", "Network Connected"); 
                  netOK = true;
              } else {
                  setViewText("net_stat", "Network Unavailable");
              }
  }
  
  public void getLocalIpAddress() {
       isNetOK();
       if (netOK) {
  
           try{
  
            InetAddress ownIP = InetAddress.getLocalHost();
            localIP = ownIP.toString();
            }catch (UnknownHostException e){
            setViewText("ip_view", "Error: unknown host");
            } catch (Exception ex) {
                setViewText("ip_view", "Something bad is going wrong");
            }
  
       }
  </code></pre>
  
  <p>isNetOK works just fine, I have been calling it separately for a day or so.</p>
  
  <p>Can someone point the way on why this code won't work?
  Is this the best approach?  My goal is to link a server at home with a domain name I got at GoDaddy.  My ip address is dynamic, but only changes every few months, or when the power goes out.  I want an app that can check the ip once every now and then (6 hours?), and notify me if it changes so I can update the DNS.  I know, hacky and slow, etc but its really just for private use and to see if I can make it work.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I tried the method in budcopter's answer below, and finally got some code working with some changes:</p>
  
  <pre><code>    public void getLocalIpAddress() {
       if (netOK) {
           try{
               WifiManager WiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
               WifiInfo wifiInfo = WiMan.getConnectionInfo();
               int address = wifiInfo.getIpAddress();
               String ipAddress = (String) Integer.toString(address);
               setViewText("ip_view", ipAddress);
            } /*catch (UnknownHostException e){
                setViewText("ip_view", "Error: unknown host");
            } */catch (Exception ex) {
                setViewText("ip_view", "Something bad is going wrong");
            }
       }
  </code></pre>
  
  <p>This returns a valid int, ten digits long.  However, when I check it against whatismyip.com, they are not even close to being the same.  The phone's LAN address is 192.168.1.110, our external ip is 75.xx.xx.xxx, the int returned is 1812048064.  This same number returns even if I power cycle the WiFi radio.  Any idea what number the WifiManager system service is giving me?</p>
  
  <p>Also, I will add a check to prevent requesting the address if WiFi is disabled, but that is trivial at the moment.</p>