首页 > 解决方案 > 从 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,则什么也不显示。

标签: androidandroid-studiolocalhost

解决方案


解决了这个问题: -

  1. 无需更改任何防火墙设置

  2. 通过同一网络连接移动设备和api运行机。

  3. 在 Windows 上使用 cmd-->ipconfig 检查运行 api 代码的机器的 IP 地址,并且“无线局域网适配器 Wi-Fi ”(IPv6)对我有用。

  4. 通过改造连接时提及完整的 url。

    改造改造 = new Retrofit.Builder() .baseUrl("http://192.168.xx.xxx:8082/hco/") .addConverterFactory(GsonConverterFactory.create()) .build();


推荐阅读