【问题标题】:Android app can't send request through NGROK on real deviceAndroid 应用程序无法在真实设备上通过 NGROK 发送请求
【发布时间】:2020-05-26 07:45:43
【问题描述】:

我知道有很多类似的问题,但我的问题很特别。 我在 localhost:3000 上有使用 NodeJS 后端的 Android 应用程序。为了在我的真实设备(以及离我很远的其他朋友)上测试我的应用程序,我使用 Ngrok 来重定向请求。 然后,在 Postman 上,我可以通过 Ngrok 到达后端。当我在 Android Studio 模拟器上运行我的应用程序时,从应用程序发送的请求可以通过 Ngrok 到达后端。在我的真实设备上,当我打开浏览器并发送 /GET 时,我也可以通过 Ngrok 到达后端。但是,当我在我的真实设备上运行我的应用程序时(我安装了从 Android Studio 生成的 apk-debug),请求没有任何响应,我在日志中看不到 Ngrok 接收它,后端也没有.

 Retrofit retrofit = new Retrofit.Builder()
            //.baseUrl("http://10.0.2.2:3000/")
            .baseUrl("http://1e2b8b83.ngrok.io/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

【问题讨论】:

  • 很好地布置了您已完成的测试!现在,您需要通过在您所做的所有写作的末尾放置一个问题来完成此处发布的过程......还包括一些来自 apk-debug 调试的日志文件......在 Android Studio 文档中查看弄清楚如何获得它们...

标签: android node.js android-studio retrofit ngrok


【解决方案1】:

我找到了: Android P 版本正在阻止对 HTTP 服务器的请求。我们应该与 HTTPS 服务器通信或使用快速修复。解决方法是创建一个包含以下内容的 xml 文件:

<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">2b8e18b3.ngrok.io</domain>
    <domain includeSubdomains="true">localhost</domain>
</domain-config>

然后将其添加到 MANIFEST Application balise 中,如下所示:

 <application
...
        android:networkSecurityConfig="@xml/network_security_config"
... />

非常感谢写这篇文章的人: https://medium.com/mindorks/my-network-requests-are-not-working-in-android-pie-7c7a31e33330

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2021-03-05
    • 2015-11-18
    • 2020-06-30
    相关资源
    最近更新 更多