[Answered ]-Connecting react native with django, fetching data

1👍

Your problem lies in the url. Your emulator and server must be connected on the same network. Mobile emulators such as Android studio or even Xcode do not make calls in localhost(127.0.0.1)

To do this, you will connect your computer to a network (internet access is not required).

You query the ip address of your machine on the network (under windows it’s ipconfig, under Linux it’s ip address)

At the end of the start the django server on the ip address. example:

python manage.py runserver 125.568.97.65:8000

You come back to react native and you replace:

useEffect(() => {
    fetch("http://125.568.97.65:8000/animalGroups/group/", {
        method: "GET",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
        },
    })
        .then((res) => res.json())
        .then((jsonres) => setAnimalGroup(jsonres))
        .catch((error) => console.error(error));
}, []);

Good luck

Leave a comment