我正在android Studio3中开发一个android应用程序,它的背面是laravel。对于本地服务器,我通过 artizan 创建了一个虚拟服务器。现在的问题是将本地服务器(127.0.0.1:8000)连接到android,为此,我在下面的界面中尝试了太多的baseUrl,例如:
10.0.2.2:8008, 10.0.3.2:8000, IP4, ............., :/
界面:
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)
}
}
}
最后,我为这个项目尝试了一个外部服务器,它可以工作,但对于本地它没有,所以伙计,如果你解决这个令人心烦意乱的问题,我将不胜感激......
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)
最佳答案
我有一个 php lumen 项目,通常使用此命令“php -S localhost:8000 -t public”开始,这对 postman 来说没问题,因为我的网址是“http://localhost:8000/api/v1/login”
在我的Android部分,我的基本url是“http://myip:8000/api/v1/login”,但它无法连接到本地服务器,并且全面调用onFailour方法,抛出异常无法连接到服务器。 所以我做了什么我使用这个命令重新设置服务器 “php -S myip:8000 -t 公共(public)”。然后它开始给出响应。 警告: 1.两台设备必须连接同一个wifi网络 2.要查找您的 IP,请转到运行类型 cmd,然后在控制台中输入 ipconfig 按 Enter
关于android - 访问 localhost laravel 应用程序路由到 android studio 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50294513/