【问题标题】:Flutter http request works on emulator, but not on real androidFlutter http请求适用于模拟器,但不适用于真正的android
【发布时间】:2020-10-14 20:21:25
【问题描述】:

http.post方法可以在模拟器上运行,但是当我在真正的安卓手机上运行时它就不行了。

Android 权限已添加。

主AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niceapp">

    <uses-permission android:name="android.permission.INTERNET"/>..
    ...
    ...

可能是什么问题?

【问题讨论】:

  • 确保权限在主清单(发布一)中。
  • @Tirth Patel 是的,它是主要的。

标签: flutter http


【解决方案1】:

这可能是因为您使用的是本地主机。检查您的网址和您所在的网络。

【讨论】:

  • 这是无线网络。网址:[10.0.2.2:8888]。我已经检查了好几次了。仅适用于模拟器。
【解决方案2】:

尝试添加以下行:

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

这部分是可选的:

在 res 中创建新文件夹作为 Android 资源目录,资源类型作为下拉列表中的 xml。

添加名为“network_security_config”的新xml资源文件

将此行放入“network_security_config.xml”

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your url</domain>
    </domain-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

编码愉快:D

【讨论】:

  • 不起作用...对吗?屏幕:yadi.sk/i/Fd3rv4TVbEHlvw
  • 将第 4 行的 URL 替换为您尝试通信的 URL。如果还是不行,那就新建一个模拟器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 2010-12-05
  • 2021-11-22
相关资源
最近更新 更多