Accessing intranet in Android emulator - android

I'm trying to test an intranet site in the Android emulator, but I can't seem to get the emulator to access our intranet. For example, the site I'm trying to access is at http://compass/messages, but trying that page in the browser gives me a Google search result page instead of the intranet site.
I can access the INTERNET with the emulator, but not the INTRANET. I can access the intranet from the host machine, and from the iPhone simulator on the same machine.
I'm assuming there's some sort of weird command line thing I need to do, but I'm pretty clueless...any ideas?

After a tremendous amount of searching with no answer, I was able to access my local dev server by substituting the domain with the ip address. For example, instead of:
http://compass/messages
use this (with your server's IP address)
http://172.33.22.1/messages
It works in the Android emulator browser, and also from a WebView in the app in the emulator. I don't know why this works, but it does for me. Hope it helps someone else.

I was having the same problem and I wasn't been able to find any solution anywhere. What I finally did (after playing around with port forwarding) was to use ssh to create a tunnel to the remote machine:
ssh -L 5555:localhost:5555 10.0.1.14
This should allow you to connect to the local machine's IP address in the emulator (10.0.2.2) and reach the other machine in your network (on the specified port).
If anyone has a better solution for accessing the local network from the android emulator I'd love to hear it.

Intranet site may require proxy.
You can set the proxy in Settings

I have solved this problem by following steps:
adb shell
set setprop net.dns 10.10.20.19(your pc's ip)
restart your emulator.

Related

failled to access to localhost using volley library [duplicate]

What can I do in the Android emulator to connect it to my localhost web server page at http://localhost or http://127.0.0.1?
I've tried it, but the emulator still takes my request like a Google search for localhost or worse it says that it didn't found the page while my web server is normally running.
The localhost refers to the device on which the code is running, in this case the emulator.
If you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2 instead.
You can read more from here.
Use 10.0.2.2 for default AVD and 10.0.3.2 for genymotion.
I used 10.0.2.2 successfully on my home machine, but at work, it did not work. After hours of fooling around, I created a new emulator instance using the Android Virtual Device (AVD) manager, and finally the 10.0.2.2 worked.
I don't know what was wrong with the other emulator instance (the platform was the same), but if you find 10.0.2.2 does not work, try creating a new emulator instance.
Try http://10.0.2.2:8080/ where 8080 is your port number. It worked perfectly. If you just try 10.0.2.2 it won't work. You need to add port number to it. Also if Microsoft IIS has been installed try turning off that feature from control panel (if using any windows os) and then try as given above.
You can actually use localhost:8000 to connect to your machine's localhost by running below command each time when you run your emulator (tested on Mac only):
adb reverse tcp:8000 tcp:8000
Just put it to Android Studio terminal.
It basically sets up a reverse proxy in which a http server running on your phone accepts connections on a port and wires them to your computer or vice versa.
according to documentation:
10.0.2.2 - Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
check Emulator Networking for more tricks on emulator networking.
For My Mac OS mountain Lion device :
http://10.0.2.2:8888
Works perfect !
If you using Android Emulator :
You can connect to your Pc localhost by these IPs :
10.0.2.2:{port of your localhost} => if you set your machine port in xamp you must use that port . In my case 10.0.2.2:2080
Also you can use your network adapter IP .In CMD write ipconfig and find your adapter ip address :
If emulator can not connect to this IPs close the emulator an open it by cold boot from AVD Manager :
If you using Genymotion :
You can connect to machine localhost by this IP : 10.0.3.2:{port number}
Or your adapter IP address as I explained above: in my case : 192.168.1.3:2080
I needed to figure out the system host IP address for the emulator "Nox App Player". Here is how I figured out it was 172.17.100.2.
Installed Android Terminal Emulator from the app store
Issue ip link show command to show all network interfaces. Of particular interest was the eth1 interface
Issue ifconfig eth1 command, shows net as 172.17.100.15/255.255.255.0
Begin pinging addresses starting at 172.17.100.1, got a hit on `172.17.100.2'. Not sure if a firewall would interfere but it didn't in my case
Maybe this can help someone else figure it out for other emulators.
Allowing PWA installation
First of all, install the Android debug bridge:
$ sudo apt install adb android-sdk-platform-tools-common
Start your Android emulator as usual, e.g.:
$ ~/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86
Only then, configure a reverse proxy on the bridge of the Android emulator that will forward localhost HTTP requests to the appropriate port (e.g. 8000) of the localhost server running on your host computer and vice versa:
$ adb reverse tcp:8000 tcp:8000
A progressive web application (PWA) being served on localhost:8000
or 127.0.0.1:8000 will be installable and connect to its service-worker.js. Whereas PWA installation is not allowed from IP address 10.0.2.2.
Caveat: adb reverse tcp:8000 tcp:8000 needs to be reissued after each Android emulator evocation.
Hence, a bash script to launch an Android emulator, followed by a reverse proxy, would look like this:
#!/usr/bin/env bash
$HOME/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86 > /dev/null 2>&1
adb reverse tcp:8000 tcp:8000
The accepted answer is correct, but didn't work in my case. I had to create the virtual device with the company VPN-client on the host machine turned off. This is quite understandable as many company networks use adresses starting with 10 (private network range), which could interfere with the special address 10.0.2.2
If you are in windows you can go to simbol system and write ipconfig and check what ip is assigned to your machine.
FOR ANYONE TRYING TO REACH A LOCAL IIS Server (ASP.NET)
For me, the accepted answer was not enough. I had to add a binding for 127.0.0.1 in the applicationhost.config, which was at the root of my ASP.NET solution.
Despite reading all the answers here and elsewhere, I have lost several hours trying to debug this issue, as the address 10.0.2.2 did not work, even in Chrome browser. If the same is happening to you, here is a step-by-step guide to try to debug and hopefully fix your issue.
Check emulator gateway is 10.0.2.2
Inside the emulated Android, go to Settings > WiFi, check if it is connected to AndroidWiFi hotspot (which represents your host computer), and then click on Advanced at the bottom, then check the Gateway address: it should point to 10.0.2.2 . If not, then you have another issue, maybe changing proxy settings can fix your issue, see here how to do that with Android Studio since 2022, as the proxy setting is now hidden away: How to configure proxy in emulators in new versions of Android Studio?
Check if your server is accessible from your host computer
Simply open a web browser and type http://localhost:<port> to see if your local web app is accessible. If not, then you likely have an issue with your local server parameters.
Check if your server is accessible from the emulator
Open Chrome browser, and point it to http://10.0.2.2:<port> (for genymotion, replace with http://10.0.3.2:<port>). If your web app shows up, great, you're done. If not, then test the other steps below to pinpoint the root issue.
Test with another server
In case your web app can be accessed from your host computer, but not inside the emulator, the root cause can be that your local server is restricting access to some interfaces for some reason, likely for security reasons.
To check this, try to use another server, just a simple HTTP server will do, such as http-server with nodejs, or python -m http.server 8000 with Python 3.
Then, try to access this simple server from your emulator's Chrome browser, eg, http://10.0.2.2:8000. If it works, then this confirms that your local server is restricting access to some interfaces. You need to read your local server's documentation to broaden permissions.
For example, in my case, my server was angular-cli (AngularJS), which by default restricts serving only to localhost. To make it work, I had to use ng serve --disable-host-check --host 0.0.0.0 instead of just ng serve, as suggested in this other question. The --host 0.0.0.0 instructs the webserver to serve all interfaces. Similar arguments can be provided to most webservers.
An alternative might be to disable some unused adapters, especially virtual ones such as VPNs.
Your Android app permissions to cleartext
Now, your web app should be accessible from inside the emulator, using Chrome app, with the URL http://10.0.2.2:<port>. The last piece of the puzzle is to add permissions in your Android app to access 10.0.2.2 and especially cleartext if your local webserver is not equipped with a SSL certificate (the most likely scenario for a local development webserver - just check if https://localhost:<port> works or only http://localhost:<port> from the host computer). This will allow your Android app to access your local webserver, just like Chrome does.
Adding specific permissions to access cleartext (ie, http://) from your Android app is necessary since Android 9 (API 28) upwards. There are several ways to configure your Android app to add this permission, see: https://stackoverflow.com/a/50834600/1121352
Conclusion
Accessing the host from the Android emulator can be tricky, but by careful step-by-step debugging, it can be possible to overcome the issue in most cases.
A last alternative, probably faster, is to get a paid subscription to services such as ngrok, but the free version is useless as they necessarily open the webapp in a web browser, outside of your Android app's webview.
I do not know, maybe this topic is already solved, but when I have tried recently do this on Windows machine, I have faced with lot of difficulties.
So my solution was really simple. I have downloaded this soft
http://www.lenzg.net/rinetd/rinetd.html followed their instructions about how to make port forwarding and then successfully my android device connected to make asp.net localhost project and stopped on my breaking point.
my rinetd.conf file:
10.1.1.20 1234 127.0.0.1 1234
10.1.1.20 82 127.0.0.1 82
Where 10.1.1.20 is my localhost ip, 82 and 1234 my ports
Also I have craeted bath file
for easy life yournameofbathfile.bat, put that file inside rinedfolder. My bath file:
rinetd.exe -c rinetd.conf
After starting this soft, start your aps.net server and try to access from android device or any device in your local network(for example Computer ABC starts putty) and you will see that everything works. No need to go to router setting or do any other complicated things.
I hope this will help you. Enjoy.
Another workaround is to get a free domain from no-ip.org and point it to your local ip address.
Then, instead of using http://localhost/yourwebservice you can try http://yourdomain.no-ip.org/yourwebservice
I know this is old, but if you find that 10.0.2.2 is not working as the computer IP, follow these instructions to find it

Accessing localhost in android application

I am making an application that needs to access the server database.
But since its still in the early stage, I have to delete the database quite often which is a problem because its used for other web applications too.
So I was thinking if I could test my application against my localhost database it would make it much easier for me.
I tried a few solutions on stack overflow but they didn't work for me so here is my problem.
I have lamp setup on my system.
In my browser, I can access localhost using 127.0.0.1 but if I try the same thing on my phone's browser it shows that the connection was refused.
Both my laptop and phone are connected to the same wifi network.
Is there some permission I have to provide that I am not aware of?
your 127.0.0.1 refers to your desktop.
Find out the local ip of your desktop (example 192.168.0.2) and then use that ip on your mobile browser
You should try to access to your laptop by using its IP address.
If your laptop is running on Windows, run the command ipconfig from a Command Prompt Window and then use the ip address the command returned on your phone's browser.
This should work.

Unable to connect to an external database [duplicate]

I try to browse localhost on my HTC Magic. I have connected my device with Eclipse via USB. When browsing http://10.0.2.2 I get "Page not available". I remember, some days ago it worked.
But on the emulator I am able to browse localhost
Any ideas?
Easier way to check is in browser of emulator type 10.0.2.2 instead of localhost.
I use my local ip for that i.e. 192.168.0.1 and it works.
to access localhost on emulator: 10.0.2.2. However this may not always work for example if you have something other than a web server such as XMPP server.
assuming that you're on the same wireless network...
find your local ip (should be something 192.168.1.x) - go to the command line and type 'ipconfig' to get this. where x is the assigned local ip of the server machine.
use your local ip for your android device to connect to your localhost.
it worked for me.
If you want to access a server running on your PC from your Android device via your wireless network, first run the command ipconfig on your PC (use run (Windows logo + R), cmd, ipconfig).
Note the IPv4 address: (it should be 192.168.0.x) for some x. Use this as the server IP address, together with the port number, e.g. 192.168.0.7:8080, in your code.
Your Android device will then access the server via your wireless network router.
I needed to see localhost on my android device as well (Samsung S3) as I was developing a Java Web-application.
By far the fastest and easiest way is to go to this link and follow instructions: https://developer.chrome.com/devtools/docs/remote-debugging
* Note: You have to use Google Chrome.*
My summary of the above link:
Connect PC with Phone over USB.
Turn on Phone's "Developer options" from Settings
Go to about:inspect URL in PC's browser
Check "Discover USB Devices" (forward Ports if you are using them in your web-application)
Copy/paste localhost required link to text field in browser and click Open.
Piece of cake
You can get a public URL for your server running on a specific port on localhost.
At my work place I could access the local server by using the local IP address of my machine in the app, as most of the other answers suggest. But at home I wasn't able to do that for some reason. After trying many answers and spending many hours, I came across https://ngrok.com. It is pretty straight forward. Just download it from within the folder do:
ngrok portnumber
( from command prompt in windows)
./ngrok portnumber
(from terminal in linux)
This will give you a public URL for your local server running on that port number on localhost. You can include in your app and debug it using that URL.
You can securely expose a local web server to the internet and capture all traffic for detailed inspection. You can share the URL with your colleague developer also who might be working remotely and can debug the App/Server interaction.
Hope this saves someone's time someday.
Combining a few of the answers above plus one other additional item solved the problem for me.
As mentioned above, turn your firewall off [add a specific rule allowing the incoming connections to the port once you've successfully connected]
Find you IP address via ipconfig as mentioned above
Verify that your webserver is binding to the ip address found above and not just 127.0.0.1. Test this locally by browsing to the ip address and port. E.g. 192.168.1.113:8888. If it's not, find a solution. E.g. https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/z4rtqkKO2hg
Now test it out on your Android device. Note that I also turned off my data connection and exclusively used a wifi connection on my Android.
Mac OSX Users
If your phone and laptop are on the same wifi:
Go to System Preferences > Network to obtain your IP address
On your mobile browser, type [your IP address]:3000 to access localhost:3000
e.g. 12.45.123.456:3000
If your localhost is not running on the default HTTP port(which is port 80), you need to specify the port in your url to something that corresponds to the port on which your localhost is running. E.g. If your localhost is running on, say port 85, Your url should be
http://10.0.2.2:85
For the mac user:
I have worked on this problem for one afternoon until I realized the Xampp I used was not the real "Xampp" It was Xampp VM which runs itself based on a Linux virtual machine. That made it not running on localhost, instead, another IP. I installed the real Xampp and run my local server on localhost and then just access it with the IP of my mac.
Hope this will help someone.
If your firewall is on, turn it off and use IPv4 to test your app in the actual device, then test your application.
I had similar issue but I could not resolve it using static ip address or changing firewall settings. I found a useful utility which can be configured in a minute.
We can host our local web server on cloud for free. On exposing it on cloud we get a different URL which we can use instead of localhost and access the webserver from anywhere.
The utility is ngrok https://ngrok.com/download
Steps:
Signup
Download
Extract the file and double click to run it, this will open a command prompt
Type "ngrok.exe http 80" without quotes to host for example XAMPP apache server which runs on port 80.
Copy the new url name generated on the cmd prompt for e.g. if it is like this "fafb42f.ngrok.io"
URL like : http://localhost/php/test.php
Should be modified like this : http://fafb42f.ngrok.io/php/test.php
Now this URL can be accessed from phone.
I use testproxy to do this.
npm install testproxy
testproxy http://10.0.2.2
You then get the url (and QR code) you can access on your mobile device. It even works with virtual machines you can't reach by just entering the IP of your dev machine.
I used ngrok but now it need registration and it also has a connections request limit. Now I'm using LocalTunnel and so far it's much better.

Android Localhost?

I know this has been asked and answered before, but I use 10.0.2.2 for the emulator. And I heard I must use something else for my real device. Can someone tell me what that is? And can I have them both run off of 1 URL. Instead of having one for the emulator and actual device.
Android Localhost?
localhost of an Android device is localhost, 127.0.0.1.
I use 10.0.2.2 for the emulator
That refers to localhost of the computer running the emulator.
And I heard I must use something else for my real device
You need to use the IP address of the computer, on whatever network that the computer and the device share. You cannot access localhost on one machine from another machine, by definition.
Of course, you also need to figure whatever it is that is running on that computer to listen to more than its own localhost.
Is there a way I can use a single IP Instead of using 10.0.2.2 for my emulator and having to use another one for my device
Not really. Moreover, neither of the values that you are thinking of are the right ones for a release build of your app, in all likelihood. There, you will need to use the domain name (or, in a pinch, IP address) of some publicly-visible server that everyone's devices can reach.
Get the ip address by running ipconfig from dos prompt and use it.
You have to use the ip address of your computer for a real device.check your ip address and try to connect with it.In my case I came to know this when I was trying to access some webapp from my android device and my web application was on tomcat container on my pc.It worked very well for me.

Android Simulator how does it connect to local IP addresses?

I am trying to implement an Android application in Windows 7 using Eclipse. I am trying to connect the Android simulator to the local test server in my company, but for some reason, it cannot connect to the test server.
If there is any settings or configurations for this, please let me know.
I have tried to do the same from a MAC machine using iPhone emulator and I am facing the same problem. How would this emulator connect to the local servers in my company as currently it all goes to live servers? What configurations are required to be done on the simulators, and how?
If I try to connect to the test server from normal Windows machine browser, I am able to successfully connect to it through the web browser; but when I try to do the same from the Android emulator browser, it cannot connect to it.
The emulator points to the live network and not the local network in my company. This is strange and I know that I need to do some settings for it, but I am not sure where these settings are done and how.
If accessing local computer:
http://localhost:8080/MyLocalServer.html // URL to use in computer browser
http://10.0.2.2:8080/MyLocalServere.html // URL to use in emulator browser
Also try using a local IP to connect to any local servers. Do not use host names.
Dont:
http://mylocalserver.org/
Do:
http://192.168.1.125:portnumber
You have to set up IP-based hosts instead of name-based.
Emulator its - VM. This use virtual network connection. I think you need before chech this connection (this connection may bee stay as NAT, Breedge, Native IP adress, Proxy).
Since you do not work iPhone emulator, most likely you, IP adress virtual network connectionб which uses Emulator, does not match the address area of the local network, and routing occurs
This may help you...
Taken from the android docs:
If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1) refers to its own loopback interface.
http://developer.android.com/guide/faq/commontasks.html#localhostalias

Categories

Resources