3

I can connect to localhost:8080 when my android app connect to my project in wampserver with 10.0.0.2:8080,but i use laravel and my web service is in localhost:8000. i want to connect from emulator to my web service in laravel server in IP 10.0.0.2:8000 but i can't connect to it.

failed to connect to /10.0.2.2 (port 8000) after 10000 ms . i tried localhost:8000 and some other IP, which IP use to connect to laravel server?

3 Answers3

1

a bit late but your issue has to do with your routing. First of all assuming you are on windows go to your hosts file and map the IP: 10.0.2.2 to 127.0.0.1

Like so:

127.0.0.1    10.0.2.2

Then if you've configured your virtual hosts map the IP 10.0.2.2 to your project directory in your httpd-vhosts.conf

Like this:

<VirtualHost *:80>
    DocumentRoot "C:/www/example/"
    ServerName 10.0.0.2
    ServerAlias 10.0.0.2
</VirtualHost>
user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • Thank you but there is a point that should be mentioned. You should run command `php artisan serve --host=x`. x is the ip you get by running command `ipconfig` in windows and `ifconfig` in linux. And in emulator you should open `x:8000`. Anyway it worked partially. It just loads `favicon.ico` and `app.js`. @user3718908 – kodfire Jul 08 '20 at 05:46
  • Tried everything for Laravel 8 App on Apache2 virtualhost i.e. `php artisan serve` with ip and normal hostname setup in `sites-available`. After trying this it worked! Thanks. Problem is if I work on website I have to reset everything in `hosts` and `.conf`. You know of a dual setup for virtualhost with same root directory? – Hmerman6006 Mar 16 '22 at 19:58
1

if you figured out this with remote server but are in difficulty to run this on local machine

1. start server as:

 php artisan serve --host 0.0.0.0

Now it can accept request from emulator

2. in android app assign to main url:

"http://192.168.0.106:8000/api"

you can get the url from ipconfig command in command prompt window

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
0

I think the easy way to connect to emulator on laravel (Android or iOS) is to simply go into the directory of your laravel_folder and run php artisan serve in the terminal (on a MacBook) or cmd (on Windows). If it doesn’t for some reason, first check if Laravel is configured properly.

If it is, and it still fails after running the php… command, check

  1. Look for a wifi or any internet connection and connect to your computer
  2. Open terminal or cmd and run ifconfig (Mac) or ip config (Windows).
  3. Look for your IP address in the result that comes from your command request.
  4. When you have got the IP address, copy it, and go to the directory of your laravel_folder
  5. When inside, type in cmd or terminal php artisan serve —-host=paste your IP address here; that should work.

NB; php artisan serve —-host=paste your IP address here on a host there is a double (-) not one. Don’t forget that.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77