未处理的异常:坏状态:平台不允许不安全的HTTP - usesClearTextTraffic无效

3

我正在使用PHP和MySQL数据库尝试在Flutter中运行基本的API调用,但是我遇到了以下错误:

E/flutter ( 7461): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform

我尝试过的方法

我已经尝试了这里提出的解决方案:Bad state: Insecure HTTP is not allowed by platform:

以及这里:https://www.programmersought.com/article/49295775092/

示例代码

home.dart

import 'package:flutter/material.dart';
import 'package:flutter_crud/env.sample.dart';
import 'dart:convert';
import 'package:http/http.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  void getData() async{
    Response response = await get(Uri.parse("${Env.URL_PREFIX}/list"));
    Map data = jsonDecode(response.body);
    print(data);
  }

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter CRUD API'),
          centerTitle: true,
        ),
        body:Center(
          child: Text('Hello'),
        )
    );
  }
}

URI前缀来自于env.sample.dart文件:
class Env{
  static String URL_PREFIX = "http://[my.IP.address.here]/flutter_crud";
}

在 debug/AndroidManifest.xml 文件中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jacobcollinsdev.flutter_crud">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:usesCleartextTraffic="true"/>
</manifest>

在主/AndroidManifest.xml文件中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jacobcollinsdev.flutter_crud">
    <uses-permission android:name="android.permission.INTERNET" /> <------- I added this line
    <application
        android:label="flutter_crud"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"> <------ I added this line
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <uses-library 
            android:name="org.apache.http.legacy"
            android:required="false" /> <-------I added this code block
    </application>
</manifest>

最后,在 android/app/main/res/xml/network_security_config.xml 中,我也创建了这个文件:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config clearTextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

到目前为止,没有什么能够解决这个错误,我已经没有想法了。任何反馈都将是极好的!


更新:我也尝试了运行 flutter clean,但没有成功。 - Jacob Collins
你解决了吗? - Abdulkarim
@abed 不好意思,不是的! - Jacob Collins
3个回答

2
android/app/src/main/AndroidManifest.xml文件中,按以下方式进行简单的修改:

1). 在application标签之前添加<uses-permission android:name="android.permission.INTERNET" />

2). 在application标签内部添加android:usesCleartextTraffic="true"


这样的更改可以使您的应用程序访问Internet,并允许使用HTTP协议。

0

您应该尝试从SSL证书域加载图像。也就是使用HTTPS而不是HTTP

因此,在您的情况下,将"http://[my.IP.address.here]/flutter_crud"替换为使用https://


-1

去除这个

class Env{
static String URL_PREFIX = "http://[my.IP.address.here]/flutter_crud";
}

添加这个

class Env{
static String URL_PREFIX = "https://[my.IP.address.here]/flutter_crud";
}

使用 https://

而不是 http://


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接