0

I've built a simple api using Flask and a react native front end. I am running both of these on separate ports on my computer. The flask api works properly and can be accessed inside a browser. When I run:

fetch('http://127.0.0.1:5000')
.then((responseJson) => {
  callback(responseJson);
})

I get an error on my android device of "Network request failed". Has anyone experiences this problem before? I have seen other have experienced similar problems but I could not find a solution online. Thanks

Ninja
  • 103
  • 1
  • 1
  • 6

3 Answers3

0

Use your IP instead of 127.0.0.1. It should work.

More details here: How can I access my localhost through Android phone?

Florin Dobre
  • 9,872
  • 3
  • 59
  • 93
0

I think you need to execute adb reverse first to connect to your development server. Run the following command.

adb reverse tcp:5000 tcp:5000

Ref: https://facebook.github.io/react-native/docs/running-on-device.html

Wanda Ichsanul Isra
  • 2,142
  • 10
  • 19
0

I had this problem for a while, and even putting my IP address didn't work. I figured out that in addition to using your IP address, you need to configure Flask to run on your machine's IP address. You can do it like this:

app.run(host='0.0.0.0')

This worked for me.