I have attempted to set up a simple web server on Android using the nanohttpd demo application without modification. The Android devices that I am using are running Android 4.0.3.
I am able to access the web server from other devices, but I am unable to connect to the web server from a browser (Firefox, Chrome, and the native Android browser) on the same Android device that the web server is running.
I have tried a variety of addresses and port combinations without success (localhost, 127.0.0.1, 10.0.0.2, the actual assigned IP address, port 8080, 8082). I have also tried having the nanohttpd application bind directly to 127.0.0.1, without success either. I have verified that I can ‘ping’ the addresses using a terminal application on the Android device, so it looks like the network connectivity is fine.
Is there a restriction on Android that will prevent a browser from connecting to the local web server instance, and if so, is there a configuration setting or permission that needs to be modified to allow this to work?
Through a bunch of trial and error, I believe that I have a handle on what is happening. When I switch from the web server application to the browser to try the request, the web server application is suspended and therefore will not accept the connection. I had assumed that the applications were running in the background, but apparently that is not the case.
I'll explore how to run the web server as a background service and see where that takes me.
After embedding the nanohttpd web server inside an Android service, all is working as I had originally intended.