Localhost running on mac.. Can I view it on my And

2020-05-11 21:28发布

Running a ruby on rails project on my mac. I need to test it on my android phone. Is there a way to view my mac localhost on my android phone?

10条回答
小情绪 Triste *
2楼-- · 2020-05-11 21:41

Found this great, free tool today - really easy to set up and works like a charm! Versions for Mac OS, Linux and Windows also available.

https://ngrok.com/

(am not associated with it in any way)

查看更多
戒情不戒烟
3楼-- · 2020-05-11 21:42

Additionally, if you want to test on Android/iOS device a PWA Apps developed with Angular, you will need to use:

ng serve --host 0.0.0.0 to start up the server CLI.

If you receive "Invalid Host Header", uses:

ng serve --host 0.0.0.0 --disableHostCheck true

查看更多
等我变得足够好
4楼-- · 2020-05-11 21:44

Here's a quick to-do to have your localhost available for tests on other devices :

1) identify the IP of your Android : select the Wi-Fi you're connected (the same as the one the Mac is on), you'll have the IP detailed on it). For the example : we suppose your Android IP is : 192.168.0.10

2) open a Shell on your Mac and edit your host :

sudo nano /etc/hosts

3) edit the file as this :

127.0.0.1 192.168.0.10

4) Identify your Mac IP : (as @Chuck perfectly explained) (in your shell) :

ifconfig

5) Open your favourite webbrowser you use on your phone and connect to your Mac IP (with the port if needed) with directly something like :

http://192.168.x.x:8000/

6) Enjoy your test :)

Notice you can do that for every support connected on your Wi-Fi.

查看更多
贼婆χ
5楼-- · 2020-05-11 21:48

As mentioned in other answers, url has to consist of the IP address of computer and not localhost. If that isn't working, before trying anything else, check if the port is correctly mentioned along with IP address when making an api call in the Android app. That is, you should be making an api call to a url which is of the format: http://192.168.X.X:80/api/..

查看更多
We Are One
6楼-- · 2020-05-11 21:54

The name "localhost" is just a loopback to your own computer. To access it on your Android, you need to find the computer's IP address.

The most general way of finding this info, which works on both Mac and Linux, is to go into the Terminal and type ifconfig. Look for either "en0" or "en1", and under that entry look for the "inet" listing. It will be something along the lines of "192.168.1.100". When you find that address, that's what you'll want to put in your browser's address bar.

(On a Mac specifically, you can go to the Sharing pane in System Preferences and it'll tell you there.)

查看更多
放我归山
7楼-- · 2020-05-11 21:57

after reading this thread (and the suggestions working!) I put together a single guide to solving this issue. This link to that guide that has screenshots for every step and where to look. Or the text is pasted below. Thank you for the help!

First off, both your phone and laptop must be connected to the same WiFi network. If you're using your phones mobile hotspot, it will still work. However, make sure to connect both devices to the same network before moving forward.

Next, collect your phones IP address. To do so, I use the Network Info II app. There is most definitely another way to accomplish this. However, Network Info II works as needed and is document for this tutorials sake.

Now open the terminal on your computer (don't worry about which directory you're within) and run the command sudo nano /etc/hosts/. After entering your system password, you'll see near the top of the terminal a string of numbers with the word localhost following. Localhost is in fact just an alias for your computer's own server address so that when you go to localhost in a browser it simply routes the http request to your local machine. In order to tie your phone into this loop, enter its IP address right between the string of numbers and Localhost.

To save this, hit control + X and then Y when prompted to save. After that, the enter key will bring you back to the standard command line.

Open a second tab in the terminal and launch a local server. I've only tested this using a simple python server, which can be run by running in the terminal python -m SimpleHTTPServer 8000. However, I'm assuming that you can launch any local server you like, being that all you'll need to reference is the port number. If you do use the simple python server, that port number is 8000.

Run ifconfig in the terminal. This will bring up a slew of information, to which you should scroll about halfway down. What you are looking for is a string of numbers that follow after an inet and before netmask within either the en0: or en1: key.

Done! On your Android, open up a browser and visit the inet number, followed by a colon (:) and the port number.

查看更多
登录 后发表回答