Android web server nanohttpd - cannot connect from browser on same device - android

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?

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

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.

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.

Related

How do I access an external IP on Android?

I would like to access my webserver running a Polymer starter kit app from an Android devices, but I don't seem to get it to work..
Polymer starter kit comes with browser-sync, which is very nice, but
I cán access my webapp on another laptop, but when I try to access it from an Android device it just keeps loading and eventually there will be a connection timeout. I triend another android device, but still the same.
Any Idea's how to access the app(external ip) from android?
Is this only for development/debugging purposes or do you want to deploy in such a scenario?
For debugging you can use Chrome's remote-debugging capabilities https://developer.chrome.com/devtools/docs/remote-debugging.
I haven't tried but I think the ADB tool allows to redirect TCP connections as well.
Otherwise you need to teach Android the IP routes (and optionally a DNS) so the device can route connection attempts to your server.

Access local network from app while phone is acting as WiFi hotspot?

I'm writing an app that needs to be able to access the web server on my laptop. The phone I'm developing on is acting as a WiFi hotspot which my laptop is connected to. Everything is working fine if I try to access 192.168.43.16 in the phone's browser.
But the app I'm developing is not able to access the webserver for some reason. I'm guessing it's trying to use the 3G connection instead, which of course will not work. If I turn off mobile data I get an error message in my app saying that it can't connect to 172.30.253.241:8799, which seems to be my ISP's MMS server (?). That makes no sense to me, but maybe there's some caching going on here? Again, accessing my local webserver with Chrome in the phone works perfectly fine.
The app is using the "Android Asynchronous Http Client" by loopj, which is built on top of Apache's HttpClient. What can I do to make this work?
The reason I want to be able to access the local webserver is because it makes developing and debugging the server api used with my app a lot easier and faster than FTP-ing to my production server on the web.
Problem solved!
What I had to do was set the proxy of the AsyncHttpClient, using setProxy(), before making the request. I set the proxy address to the local IP address of my laptop running the web server.
client.setProxy("192.168.43.16", 80);
Now it seems to be working as expected.

Android device not accessing web service on local WIFI

I have made an asp web service and I am accessing it from my android application through ksoap2 library. I have successfully accessed the service from emulator by 10.0.2.2:9000/Service1.asmx.
Now I am trying to access it from my android device but I am not able to access it.
I have already hosted the service on IIS and now I can access it from emulator by 192.168.xx.xxx/Service1.asmx.
The problem is still the same Can't see any result in my android app on device that is connected to the same LAN through WIFI, although I can see my web service through mobile browser now. In addition to that I have edited the web.config file to allow all systems on LAN to invoke my web service methods.
I just checked the error. Error toast is showing java io.IOException on device still runs without any error on emulator
I would suggest to check whether 192.168.xx.xxx/Service1.asmx. is reachable via android mobile browser. I doubt that your device could be in some other ip group.

How to access a URL on port 80 via android browser

my android device cannot access a URL:
http://myip:8080/alias
I tried another port of my service (80, running a service apache too) and works!
The tests, i'm doing from web browser of my device.
From my computer (on same network), both URL works!
The problem like a firewall. I cant access a port different of 80.
Thanks,
It's likely that the problem is related to your web service rather than your android device, as you should be able to access pages on non-80 ports without any issue.
Have you tried using fiddler or another debugging proxy to check the response from the server?
If not, this may be useful
http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigureForAndroid
Edit: also, you mention that you can view it on port 80 on your android device, perhaps try disabling your firewall on your PC?

Not able to access the webservice available in same network in Android

I am using Android 2.1 tablet. I have created simple application which access my own webservice. It works well in emulator. But it fails on android device. I am getting timeout exception. I have internet permission and timeout permission as mentioned is few forum message. I have disabled firewall for my network. Though I couldn't ping it from Terminal emulator to check the network availability.
Also I tried access the webservice base url via browser in android device. It doesn't work. I have webservice running on port 9080 in a laptop which is connected to same wifi network as android device.
I am absolute clueless. Any help would be highly appreciated.
If the emulator was running on the same machine as the Web service, then perhaps your Web service is only accepting connections from localhost.

Categories

Resources