I am new to android and backend development and came in a problem today. I have setup rails server on my computer and localhost:3000 shows ruby page. My android device (connected by usb) main activity has a webview and I tried localhost:3000
Use Your IP Address Instead of Localhost
Instructions if you're on a Mac. Other OS's, do something comparable.
Connect your computer and Android tablet to the same Wifi connection.
Open System Preferences.
Go to the Network section.
Click on your Wifi connection and copy your IP address from that window:
Make sure your Rails server is running on port 3000.
On your tablet, point to your computer IP address with port 3000,
192.168.0.14:3000
Hope that helps!
/ JP
Just simple, you should try to do these steps same as below:
First, you type this command in order to show your ip address: ifconfig
Second, in your console, type this command: rails s -b your_ip_address
For example: rails s -b 192.168.1.102
Finally, open browser on your android and type: 192.168.1.102:3000
You can read this rails official to understand clearly.
http://edgeguides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server
http://smyck.net/2007/03/11/how-to-bind-webrick-to-any-ip-address/
You need to bind your webrick to ip address of your ethernet or wifi adapter.
if your laptop and device are on the same wifi, just connect to the hostname or IP of your laptop http://[laptop Ip or hostname]:3000
Related
I'm developing a web application (I'm using MacOs) and I would like to see the screens on my android mobile device. Although both devices connected the same network I couldn't connect computer's IP address and local port on my phone. I'm thinking of this issue is showing up by MacOs. Because when I run same project in to Windows machine I can connect to the IP address with my phone. I've tried a couple of things but I couldn't solve the issue. What should I do be able to connect my phone to the computer's IP address ?
Go check how can you create a server with a "real world" link using ngrok (https://ngrok.com).
I assume Nodejs application only bind to your local (MacOS) and only can access from your laptop. The option is using Nginx or Ngrok to open application to LAN or world.
If you want allow other devices from LAN to connect to your Mac, you can go to System Preferences > Security & Privacy > Firewall then Off.
I have a server in xampp in os x laptop, when I type: 10.0.2.2/myphpServer in the emulator web navigator, goes ok, but from a usb-connected real android device, I can not connect.
Check public ip of your pc by ifconfig command in terminal
then use YOUR_IP/myphpServer
If you are in local network (router or LAN) then use your local_ip/myphpServer (make sure your mobile is also in same network)
If your mobile doesn't have internet connection then go for Android USB Tethering
You need to connect both device to same network to access localhost in Android device.
Better use the IP Address of your laptop type in cmd ipconfig search for Local Area Connection: IPv4 Address
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.
I am developing an android app. For local testing I have created a django server on my laptop. My problem is I am not able to call django apis from app code. For example, if I want to call any django server api from desktop I write "localhost:8000/polls/link/2/". Now how to replace this "localhost part of url" if calling same api from mobile. And also my desktop is connected to internet by the same mobile hotspot. So basically both desktop and phone are on same network.
My ifconfig command on desktop shows
And my desktop is Mac and mobile is Samsung core duo
For developing you can use your device's inner IP address
in terminal:
$ ifconfig | grep inet
you will get:
inet addr:10.0.0.1 ...... use that ip with django runserver
$ python manage.py runserver 10.0.0.1:8000
from android use:
"http://10.0.0.1:8000/" as your url String
can you see the index.html on your phone browser?
localhostIP:8000
if you can, then all should be fine, if not, there is a configuration issue,
is your website on your local server accessible from the outside?
How to access the local Django webserver from outside world
good luck
If I understand your question correctly, you have a Django installation on your laptop and you are able to hit a specific page/service locally using "localhost:8000/polls/link/2/" and it works fine. Now you want to access it from your mobile. Correct?
Replace the "localhost" with IP address of your laptop. For example, "10.0.0.1:8000/polls/link/2/"
You will find your IP address in System Preferences -> Network
You have to set multiple things for access your API at your local network.
Go to terminal and type ifconfig and get your inet address.
Then add this inet address in allow_host of your project setting.py file.
Start your server by using command ---> python manage.py runserver 0.0.0.0:8001 or any other port which is free.
I am assuming your inet address is 192.168.1.10
Then go to your browser and type
192.168.1.10:8001/your_app_name/your_url_of_view
Firstly run the django server by typing python manage.py runserver 0.0.0.0:8000. Then check the ip of your private network i.e wlan ip using ifconfig command then simply replace your localhost text with that ip while calling API.
But above all make sure that your phone and lappy should be in same network.
I would like to add on to all the answers
if you want to run django server on your phone follow the steps
step 1 -> run powershell type ipconfig for windows ( for mac type grep inet)
ipconfig
step 2 -> find your phone's ip address it will be of the form 198.168... type in the ip4address in the results you got from step 1.
you should be connected to the same wifi or lan to do so. So connect through hotspot ok!
step 3 -> now add this ip4 address to your setting.py of django project
ALLOWED_HOSTS = ['192.168.43.103']
**remember to add your ip4 address rather than this one **
step 4 -> after you found your ip4 address of phone now run your django project with some port. Say (your ip4 address):port here 198.168.43.103 is my ip4 address and 8000 is port (you can choose any port but i recommend make it upto 4 digits)
python manage.py runserver 198.168.43.103:8000
your operating system might ask permission to allowed your network to be viewed on other devices since it is safe for development purpose, allow it to access
step 5 -> open your android to which your are connected with network and open its browser and open your link to which you run the server in django. In my case it is -
198.168.43.103:8000
you can see the project voila!
for support mark this answer as useful
Have you try this?
or checking firewall options.
manage.py runserver 0.0.0.0:8000
I want to debug my android application, I connect the android device to my PC using the USB cable. In my application there is a button to connect with localhost, ip for localhost is 10.0.2.2 and the port is 8080, I have read that when debugging on mobile, the ip 10.0.2.2 is the localhost for android device and not for my PC, so what changes should I make to the ip instead of 10.0.2.2? or do I have to make another change?
In this case my android device is sony ericsson xperia arc s.
Google has added support in Chrome 29 and higher to use reverse port forwarding to access a website hosted on your local development machine through the USB cable on Chrome for Android. Setup instructions can be found at the following URL:
Android Remote Debugging - Reverse Port Forwarding
Tools for Web Developers - Access Local Servers
As of desktop Chrome 30 Reverse Port Forwarding is no longer an experimental feature in Chrome. It can be accessed by typing about:inspect in the address bar of your PC, and by clicking the "Enable port forwarding" check box and clicking the "Configure port forwarding" button located to the top right of the window.
Once that is done, connect your mobile device via USB. Open Chrome on your mobile device to localhost:8000 (or whichever port you have configured on your local server).
The Reverse Port Forwarding functionality will make sure that your Android device now sees your PC's localhost.
As 10.0.2.2 is your system (pc)'s local host address (from emulator only). Actually android doesn't recognized localhost in url. so 10.0.2.2 is for that meant. Also for android device loopback address is 127.0.0.1.
Your url with 10.0.2.2 is correct. Also you can use Static IP of your system.
Just check for
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in your application's manifest file.
EDIT:
Here you are using port 8080 so, try with adb command on your host machine.
adb forward tcp:8080 tcp:8080
Also please elaborate on this line "i want to debugger my application on my mobile".
Note:
If you are going to test on real device use your Network IP of system (PC).
For that you need to make some changes in your xampp server... Assign 1 static IP address to your system and then you need to put your xampp server in online mode. after that you can use that ip address in your android application instead of 10.0.2.2. Works fine for me as i am using my localhost with my android application.
You can share your Mac's Internet connection over Wi-Fi. Then your Android app can connect to a Servlet running on the Mac with HTTP over Wi-Fi. The steps are:
Run System Preference on Mac
Goto "Sharing" tab
Turn on "Internet Sharing"
Select "Ethernet" in the "Share your connection from" combo
Select "Wi-Fi" in the "To Computers Using" list box
Use "Wi-Fi Options..." button to configure Wi-Fi security. Now your Mac is a Wi-Fi server, and it is sharing its Ethernet Internet connection.
Configure your Android device's Wi-Fi to connect to your Mac (in Settings command)
On your Mac, goto the Network tab in System Preferences, and select Wi-Fi in list to find out the IP address of your Mac on the Wi-Fi network (for me it was 169.254.66.223)
In your Android App you can now connect to the Servlet in your Mac with "http://169.254.66.223:8080/YourServer/YourServlet"
I think you have two options
The first one is using 10.0.2.3 when you use your real android device.it works for me.
Your Second opt is creating hotspot from your pc and connect your android device to the hotspot.
Find the ip address using cmd type "ipconfig" replace localhost with the ip address.
Thanks.