-1

so I have a function that works when a button is clicked, and inside that function there's a fetch method to post some data to the backend (flask) when I'm using a real android device (note 9 and note 3 ) it doesn't post anything,and I don't get any errors, meanwhile the android emulator is working fine and posts the data.

the solutions I found for that problem is to use internal IP address instead of localhost. but it didn't work for me too. and if I used localhost or 127.0.0.1 I get " network request failed". I also tried to use axios but I get the same results.

  getloc = () => {
   setInterval(() => {
   fetch("http://192.168.1.107:5000/api/echo", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      x: "0",
      y: "0"
    })
  })
    .then(response => response.json())
    .then(responseJson => {
      this.setState({
        xMaster: responseJson.x
      });
      this.setState({
        yMaster: responseJson.y
      });
    })
    .catch(error => {
      console.error(error);
    });
}, 3000);
};

1 Answers1

1

Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on your machines IP address.

Documented on the Flask site under "Externally Visible Server" on the Quickstart page:

Community
  • 1
  • 1
Hamza Waleed
  • 1,334
  • 10
  • 12
  • but this's react not flask. – Junius L Jun 27 '19 at 12:02
  • @JuniusL. from the doc that hamza just posted it says "If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer. If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:" – Muhammad Lebda Jun 27 '19 at 12:07
  • Yes, exactly. :) – Hamza Waleed Jun 27 '19 at 12:08