android - 从 android 移动设备连接到 localhost rest api
问题描述
我在java中使用spring boot开发了一个rest api,它在我笔记本电脑的8082端口上运行。
接下来,我创建了一个 android 应用程序来使用来自我的 rest api 的数据。我对此进行了测试,当我使用 android 模拟器时它工作正常。
但我需要它在我的 android 移动设备上运行它。怎么做?
我已经搜索过它并且多次询问过它,但似乎没有一个对我有用。
我尝试过 的我尝试通过我的笔记本电脑 Ip 和 Android 代码中的端口连接到我的笔记本电脑浏览器
为两个设备使用相同的 wifi 网络
//192.xxx.xx.xx:8082
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.xxx.xx.1:8082")
.addConverterFactory(GsonConverterFactory.create())
.build();
结果 应用程序运行,但大约 10 秒后出现连接超时错误,如果我使用“http://localhost:8082/company/employees”url,则什么也不显示。
解决方案
解决了这个问题: -
无需更改任何防火墙设置
通过同一网络连接移动设备和api运行机。
在 Windows 上使用 cmd-->ipconfig 检查运行 api 代码的机器的 IP 地址,并且“无线局域网适配器 Wi-Fi ”(IPv6)对我有用。
通过改造连接时提及完整的 url。
改造改造 = new Retrofit.Builder() .baseUrl("http://192.168.xx.xxx:8082/hco/") .addConverterFactory(GsonConverterFactory.create()) .build();
推荐阅读
- swift - 使用 UITextField 和 UITextView 的错误
- python-3.x - 得到结果后,还提到了内存位置
- javascript - 未调用 setState 方法
- java - 如何解析以“/”开头的 JSON 数据?
- fortran - 更改 MPI 通信的布局:从 MPI_ISEND/MPI_IRECV 到其他
- systemd - systemctl 服务文件中斜线前的连字符是什么?
- .htaccess - 重写 URL 后找不到对象错误
- angular - Ionic4 使用 Angular 路由策略(默认)?
- python - 在python中使用Json时出现Keyerror
- flutter - 如何将动态小部件添加到字幕?(对于 ListTile)