skip to Main Content

I am using a Django server, and a react native frontend (with expo).

I can access the server fine on my browser and with the iOS simulator (I use a Mac), however, on the android simulator, I get a network error, and it can’t connect to my server.

Here is the react native code that works fine:

  componentDidMount = () => {
    fetch("http://127.0.0.1:8000/api/events/?featured=true", {
      method: "GET",
    })

What am I doing wrong?

2

Answers


  1. Chosen as BEST ANSWER
    1. I added my machine's IP to ALLOWED_HOSTS in my Django settings.py
    2. Then I ran my server on my machine's IP with the command: ./manage.py runserver XX.XXX.XX.XX:8000 (replace the X's with your machine's IP)
    3. And then I changed all my react native functions to the following:
    const root = "XX.XXX.XX.XX"
      componentDidMount = () => {
        fetch(root+"api/events/?featured=true", {
          method: "GET",
        })
    

    Replace the X's in the above code with your machine's IP-address.


  2. Use 10.0.2.2 to access your actual machine for the android emulator.

    when you use the emulator, localhost (127.0.0.1) refers to the device’s own loopback service, not the one on your machine as you may expect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search