HTTP POST in android device with localhost and port - android

I want to make a post on http://xx.xx.xx.xx:8083/AuthenticationService.svc/SignIn in an android app. The x's represent the localhost and 8083 is the port. This works very well on the computer, but how do I get the localhost numbers (and possibly the port, if it differs) in an android app?
I've only found answers on how to get the emulator running by using a certain number for localhost, but not the straightforward answer for an actual android app ran on the device (phone).
I mention that I know how to make the post, I just need to know how to form the URL mentioned. Thanks

Once an app goes off to a device, it needs to be pointed to the IP address (or DNS name) of the server you want it to talk to. In Linux you can find the server's IP address with ifconfig. Or in Windows, ipconfig.

Related

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.

Run ASP.NET application on server and access from remote computer/phone

I am trying to debug my visual studio 2012 asp.net project from my phone and have followed all steps here and here.
However none of them work, can't even connect from my other laptop. When I add my public IP or use * IISExpress doesnt even seem to pick up on it (Can't find it in IISExpress->show all applications). But When I used my local IP at least it showed up there and I could connect to it from my own computer, but no other computer/phone.
The cmd commands are successful, I add the extra binding, I open the firewall. Dont know what is wrong.
Which IP (local/public) should I use? what should I type in as URL when I want to test it? I am trying http://IP:port and http://IP:port/Default.aspx.
EDIT: Using this solution I got it to work on my laptop, however I still can not get it to work on my phone.
Basically, the points mentioned in the links you have cited are enough for enabling communication with the phone. But here is one point to make sure (since you have not mentioned it): You have to make sure that both the server and the clients (in your case, the machine that runs the visual studio and your phone and the other laptop) are on the same wireless network. To find out the IP that your server/development machine is currently using, you can issue ipconfig command in cmd and look at the Wireless Network Adapter IPV4 address. For example, if your laptop IP address is 192.168.1.2, you should use http://192.168.1.2:8080/Default.aspx (assuming that your site runs on the port 8080).
You use the local IP if you're on the same network as your server (for example, use WiFi on your phone and join the same network that your server is on).
You use the public IP if the device is outside your network, and you have to configure the firewall to forward the correct ports to the local IP. That's NAT traversal. This would be if you're using the phone's cellular data connection, or you're using a WiFi or wired network that's different from the one your server is on.
Your URL should always be of the format <protocol>://<IP or DNS name>:<port>/<path>. However, you can omit the IP (and the colon) if you're using HTTP on port 80, or HTTPS on port 443. And you can omit the path if you're accessing the root of the site, and your site has a page correctly configured for the root (either via default document in IIS, or route defaults if you're using routing features).

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.

How to access the localhost via android mobile device?

I have a asp.net development server(localhost). I want to access the localhost through my android mobile phone(Sony Ericsson XPERIA). Because I have some web service methods that I want to access. I have done it using the emulator(by giving 10.0.2.2:portNo). But I don't know how to access the localhost via the mobile device.
Your help is much appreciated.
Thank You
Update: - 14-09-2012
Guyz I really need your help on this issue.....please help me.. I have a wireless router and my laptop is connected to that, and also my laptop has a static ip given to it.
I have an android phone and it also connected to the wireless router perfectly. And my web site is hosted in IIS7 and it is assigned with a port.
What I need is to access the web services resides in the web site through my android device(using the application).
I have attempted many times giving laptop IP(static IP) and 10.0.2.2 IP also. But none works. And I am really confused now.
If you know any complete tutorials or if you did this before please post it. Your help on this is really appreciated guyz. (please ignore my English if its wrong) Thank you.
You have to change the path that you have given while accessing your
Web Service.
Replace localhost by your own ip address.
You can fetch your ip address through cmd.
goto cmd and type ipconfig.
If your mobile phone and your web server are on the same WiFi network, you can do as was suggested in other comments - figure out the IP address of your development server using the ipconfig command and then use that address on your phone. Note that you may need to modify the firewall settings on your development server to allow incoming connections.
However, if your mobile phone is on 3G or the devices are just not on the same network, things get more complicated because you probably do not have a public IP on your development server and the devices won't be able to see each other.
In that case you either need to reconfigure your development server's local router (google for instructions on "port forwarding") or use a tool like https://pagekite.net/ (disclaimer: I wrote that!) to give your development server presence on the public Internet.
Good luck!
Simply visit 10.0.2.2 in your Android-browser. If this does'nt work, disable your firewall aswell.
You need to connect your Android Device ( Phone or Tablet ) via WiFi Connection with your local network.
Once the connection is establish, you can check the connection in browser by opening url http://xx.xx.xx.xx:portNo ( as answer by EvdTempel )
you need to set address as "10.0.2.2:portNr"
portNr = the given port by ASP.NET Development Server
Hey I had the same issue and resolved it form the information I found at https://forums.adobe.com/thread/1109558
use the 4 answer "4. Re: How to access localhost on ASP.NET Website from Android device"
worked for me :)
Simply visit http://your-system-ip-address/your-site name.
If it does not work then disable your firewall as well.

Categories

Resources