我在登录时遇到问题,在我的模拟器中它正常工作,但当我生成apk并在手机上测试时,API不工作。
我已经在.xml中分配了Internet权限,并进行了使用Internet映像的测试,所以我放弃了这些权限。
我不知道是因为我使用的是http而不是https,还是有人知道他们告诉我这里发生了什么,我附上我的代码
代码:
void _login(BuildContext context) async {
if (!_loading) {
setState(() {
_loading = true;
});
}
//print(_username.value.text);
//print(_password.value.text);
var url = Uri.parse(
"http://taquillamedicina.mdgsystem.com/index.php?route=api/loginapp");
String var1 = 'vipmedico';
String var2 =
'xxxxx';
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Map data = {
'username': var1,
'password': var2,
'usernameapp': _username.value.text,
'passwordapp': _password.value.text
};
var jsonResponse = null;
var response = await http.post(url, body: data);
//print(response);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
// print(jsonResponse);
if (jsonResponse != null) {
setState(() {
_loading = false;
});
sharedPreferences.setString("client_id", jsonResponse['client_id']);
if (jsonResponse['error'] == '1') {
Navigator.of(context).pushReplacementNamed("/list");
}
}
} else {
setState(() {
_loading = false;
});
print(response.body);
}
}
要克服这个问题,您必须将android:usesCleartextTraffic="true"
添加到AndroidManifest.XML
文件中。但正如建议的那样,这并不是解决问题,而是把它掩盖起来,但会暂时完成你的任务。
升级Flutter 2.0后,我的应用程序关闭,我在Anroid下添加了>Src>Main>AndroidManifest.xml
android:usesCleartextTraffic="true"
API和应用程序正在使用