2

I'm developing an android app in android Studio3 that its back is laravel. For the local server, I created a virtual server by artizan. Now the problem is connecting local server (127.0.0.1:8000) to android and for this, I tried too many baseUrl in bellow Interface like:

10.0.2.2:8008, 10.0.3.2:8000, IP4, ............., :/

Interface:

interface ApiInterface {

    @GET("stocks.json")
    fun getStocks(): Call<StockResponse>

    companion object {
        val BASE_URL = "http://192.168.1.106:8000/api/"

        fun getClient(): ApiInterface{
            val retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()

            return retrofit.create(ApiInterface::class.java)
        }
    }
}

And finally, I tried an external server for this project that it worked, But for local it didn't, So buddy, I would be appreciated if you resolve this distraught issue...


UPDATE: Guys thank you for your response, I really tried many ways and finally, I found out that laravel api routes fail in the android studio and it is not founded, It means:

http://192.168.1.106:8000/file.json  (Worked-> specific file in local server )
http://192.168.1.106:8000/api/file  (Not Worked-> laravel route api)
  • You did not tell where your Android app is running on. – greenapps May 11 '18 at 14:56
  • I try android studio emulator and even test Genimotion and an external android device –  May 11 '18 at 15:06
  • So just to make sure: You are running your server on a machine. You can access it via 127.0.0.1:8000 on this machine. You are running an Android app in the emulator on the same machine. And you tried accessing the server via 10.0.2.2:8000. The above config should work, and deviations in any of the steps could cause an issue. – Dennis K May 11 '18 at 21:16
  • I really tried many ways and finally, I found out that laravel api routes fail in the android studio and it is not founded... –  May 12 '18 at 04:07

3 Answers3

3

I have a php lumen project which I started usually using this command 'php -S localhost:8000 -t public' this is ok with post man as my url is "http://localhost:8000/api/v1/login"

In my Android part my base url is "http://myip:8000/api/v1/login" but it can't connect to the local server and comprehensively calling onFailour method which throwing an ecxeption can't connect to the server. So what I did I restrat the server using this command "php -S myip:8000 -t public". Then it start giving response. Caution: 1.both device device must be connect with same wifi network 2.to find your ip, go to run type cmd then in console type ipconfig hit enter

Saddan
  • 1,546
  • 16
  • 19
1

I'm not certain the artisan webserver listens to any requests from outside localhost. You may be able to configurate it to behave that way. Alternatively, you can host your Laravel project locally through Apache or Nginx, and configure the webserver to accept connections coming from your local network.

https://stackoverflow.com/a/24011132/9776011

https://serverfault.com/a/667800

Wouter H
  • 31
  • 3
0

I found it starting from here :D .
first, u can't use val BASE_URL = "http://[ip_add]:[port]/api/"
you change it to something like this:
private static final String BASE_URL = "http://my_ip_address/path/to/project/folder/relative/to/server/root"
so, it would be like this :

private static final String BASE_URL = "http://[my_ip_address]:[port]/[project_folder_name]/public/api/"


and it's done ;) ... hope it helps !

joe_inz
  • 998
  • 2
  • 13
  • 30
  • I try it but don't work. what did you mean of [project_folder] ? my project is in this path on my computer: `C:\Users\NP\PhpstormProjects\halAmooz` . Should I put this path to [project_folder] ? I try this : `BASE_URL ="http://192.168.43.67:8000/halAmooz/public/api/"`. I run sever through `php artisan serve` command in phpStorm terminal. – maryam Oct 22 '18 at 15:07
  • yes it should be the same as what you have provided in the comment. – joe_inz Oct 23 '18 at 12:56
  • I run apache server and put the laravel project to `apache\htdocs` path. When I test `BASE_URL ="http://192.168.43.67:8000/halAmooz/public/api/"`, I receive `404 page not founded` error.How to run my project to apache server? I only access to public file of my laravel project in my browser and other route don't work – maryam Oct 26 '18 at 20:11
  • make sure that there is no typo in your BASE_URL, same for the posrt and ip address . – joe_inz Oct 28 '18 at 10:32
  • @maryam , you need to have .htaccess file with the configuration maybe in order to make other route functioning well – Lalat Putih May 03 '23 at 04:55