简体   繁体   中英

Why can't I connect to an API when I generate the APK?

I have a problem with a login that I am doing, in my emulator it works correctly but when I generate the apk and test it on the phone, the API does not work.

I already assigned the Internet permission in the.xml and made tests to use Internet images and everything fine, so I discarded the permissions.

I do not know if it gives me an error because I use an http and not an https, or if someone has an idea of what is happening here they tell me, I attach my code

Code:

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);
}
}

To get over this, you will have add android:usesCleartextTraffic="true" to your AndroidManifest.XML file. But as advised, this isn't solving th eproblem, it's sweeping it under the carpet, but will get your task done for now.

After I upgrade the Flutter 2.0 my app is down and I add under Anroid>Src>Main> AndroidManifest.xml

android:usesCleartextTraffic="true"

API and app is worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM