Home > Back-end >  I wouldn't be able to connect to the Api Rest (made in python Flask Localhost) from my cell pho
I wouldn't be able to connect to the Api Rest (made in python Flask Localhost) from my cell pho

Time:04-19

Hi how are u? I wanted to know why I wouldn't be able to connect to the Api Rest (made in python Flask Localhost) from my cell phone (I use it as a divice with the cable, I don't use the emulator). I don't know if maybe something needs to be configured in android. I read somewhere that you have to put it in the AndroidManifest.xml

android:usesCleartextTraffic="true"

I put it in but it still doesn't work. I have tried using “localHost” “10.0.0.2” “http://127.0.0.1:8000/” but nothing works. I don't know what I'm doing wrong, I send you my code

class AuthService extends ChangeNotifier{
  final String _baseurl= 'http://10.0.2.2:8000';
  Future<String> login(String email, String password) async{
    final Map<String,dynamic> authData={
    'email':email,
    'password':password,
    // 'returnSecureToken':true
  };

  final url= Uri.http(_baseurl, 'api/auth'
  // {
  // 'key':_apiToken
  // }
  );

  final resp= await http.post(url,body:json.encode(authData));
  final Map<String,dynamic> decodeResp =json.decode(resp.body);
  print(resp);

  return decodeResp[0];

  }
}

If you can help me I would really appreciate it, I'm really going crazy trying to find the answer.

CodePudding user response:

I don't know much about flask, however:

  1. Are both devices on the same network? This may be a stupid question, but sometimes they aren't.
  2. Is the IP address you put the actual private IP address? You can do ip addr on Linux and ipconfig on Windows to get your private IP address.
  3. Try "0.0.0.0". In http.server, and possibly in Flask, 0.0.0.0 means any connection. You can also view my similar question here.

CodePudding user response:

i find the solution , in Flask u must be to write the host

if __name__ == '__main__':
    # load_dotenv()
    app.run(debug=True,port=8000,host="your Ip")
  • Related