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)
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.
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 !
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
来源:https://stackoverflow.com/questions/50294513/accessing-localhost-laravel-app-routes-to-android-studio-failed