0

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?

KenSt
  • 19
  • 4

3 Answers3

1

1) You need to grant internet permission in AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />

2) Also the port 80 will not be available without root.

See this question for more information.

3) Disable any VPN or proxy app or data compression on your device

Community
  • 1
  • 1
Robin Eisenberg
  • 1,836
  • 18
  • 26
  • Thank you for the answer. Yes, I have granted INTERNET permission in the manifest, and I am using port 8080 by default. All works fine when I access the web server from another device, like my desktop PC. I just cannot access the web server from the same device. – KenSt Oct 06 '14 at 14:56
  • What do you get when you try from the device ? A 404? Also can you post your code please? – Robin Eisenberg Oct 06 '14 at 14:56
  • I am using the sample code without modification from here, before I extend it with my own implementation: https://github.com/NanoHttpd/nanohttpd/blob/master/samples/src/main/java/fi/iki/elonen/HelloServer.java. The TCP connection to the web server is never established, and I get a "Unable to connect" message on the browser – KenSt Oct 06 '14 at 15:26
  • Do you perhaps have a proxy app or vpn on your device? What manufacturer/device are you on? – Robin Eisenberg Oct 06 '14 at 20:35
1

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
KenSt
  • 19
  • 4
0

Change the request url to http://localhost:port or if you want to use IP address for localhost on emulator you can use the documentation here.

Samson Ayalew
  • 359
  • 3
  • 8