【问题标题】:Connect to localhost rest api from android mobile device从 android 移动设备连接到 localhost rest api
【发布时间】:2020-10-27 22:50:47
【问题描述】:

我在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”网址,则什么也不显示。

【问题讨论】:

  • 您可以在移动设备的 Chrome 中访问192.xxx.xx.1:8082 吗?
  • 不,我试过了,也出现连接超时错误。
  • 意味着您无法在笔记本电脑之外访问您的服务器。你需要先解决这个问题。
  • 你能给我一个想法,我可以尝试实现什么。
  • 如果它在模拟器上运行,那么您唯一需要更改的就是 IP 地址。

标签: android android-studio localhost


【解决方案1】:

解决了这个问题:-

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

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

  3. 在windows上使用cmd-->ipconfig检查api代码运行机器的ip地址,“Wireless Lan Adapter Wi-Fi”(IPv6)对我有用。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 2013-10-30
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多