1

i am running NanoHTTPD in my android application. i am serving some contents using this server.

I am also trying to view the contents in a web view (again in my application ).

i use the below code to get the ip address using the WifiManager as suggested here https://gist.github.com/komamitsu/1893396

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
    final String formatedIpAddress = String.format(Locale.US, "%d.%d.%d.%d", (ipAddress & 0xff),
            (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

This code works when wifi is there but not during when phone is offline by giving the ip as 0.0.0.0 and displaying the error as ERR_PROXY_CONNECTION_FAILED.

One approach i can think is to use the localhost when network connectivity is not there. Is there any other way i can solve this problem ?

Thanks and Regards,

Saurav

saurav
  • 5,388
  • 10
  • 56
  • 101
  • If you can use localhost when the network is gone, why not always use localhost? – nkorth Aug 20 '15 at 08:55
  • well i might need to serve the content outside of my device someday later...for that ip might be useful – saurav Aug 20 '15 at 08:57
  • Ok, then it should be as simple as trying to get the IP address, and if that fails, using localhost. If there's no network, you won't be serving the content outside of the device anyway. – nkorth Aug 20 '15 at 09:13

0 Answers0