3

So I have a backend with django and I am using react native for frontend. I added a emulator in android studio.

And when I start the backend like: python manage.py runserver I can run also the frontend with expo start.

But now I want to fetch the data with react native. So I start the backend with: python manage.py runserver 192.168.1.68:19000 - settings file of django:

INSTALLED_APPS = [
    'corsheaders',
    'DierenWelzijnAdmin.apps.DierenwelzijnadminConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ALLOWED_HOSTS = ['192.168.1.68', '127.0.0.1', ]

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = [
    "http://localhost:8081",
    "http://localhost:19006",
    "http://localhost:19002",
    "http://192.168.1.69:8000",
    "http://192.168.1.68:19000",
]

CORS_ALLOW_METHODS = [
    "DELETE",
    "GET",
    "OPTIONS",
    "PATCH",
    "POST",
    "PUT",
]

CORS_ALLOW_HEADERS = [
    "accept",
    "accept-encoding",
    "authorization",
    "content-type",
    "dnt",
    "origin",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
]


And react native service:

export const fetchCategoryData = async () => {
    try {
        const response = await fetch("http://192.168.1.68:19000/animal/categories/main_groups/", {
            method: "GET",
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json",
            },
        });
        if (!response.ok) {
            throw new Error("Network response was not ok");
        }

        return await response.json();
        //setCategoryData(data);
    } catch (error) {
        console.error("There was a problem with the fetch operation:", error);
        throw error;
    }
};

And when I then start the react native app with expo start and I do a r after a.

I get this message:

warn No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.

The emulator I installed from android studio is: Pixel 2 XL and if I do: adb devices I see the emulator:

List of devices attached
emulator-5554   device

And I already whiped the data several times from the emulator.

Question:

How to run the react native for fetching data from the backend?

mightycode Newton
  • 3,229
  • 3
  • 28
  • 54
  • 1
    Does this answer your question? [No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB](https://stackoverflow.com/questions/62937553/no-apps-connected-sending-reload-to-all-react-native-apps-failed-make-sure-y) – Ankit Tiwari Feb 13 '23 at 16:11
  • But I am running on a emulator with android – mightycode Newton Feb 13 '23 at 16:14
  • Check the provided link for all available answers; there is an issue with your IP. I think your emulator is not able to connect to the provided IP. – Ankit Tiwari Feb 13 '23 at 16:16
  • @AnkitTiwari. But the ip is correct: I did a ipconfig: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::3a4e:7938:aa45:3ef%10 IPv4 Address. . . . . . . . . . . : 192.168.1.68 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 – mightycode Newton Feb 13 '23 at 16:19
  • @AnkitTiwari. And if I start the backend and go to: http://192.168.1.68:19000/animal/categories/main_groups/ the api call works. – mightycode Newton Feb 13 '23 at 16:22
  • 1
    If you've access to `adb` command [`adb reverse tcp:9000 tcp:9000`](https://stackoverflow.com/questions/62062291/adb-command-similar-to-reverse) this tells you emulator to listen port 9000 on your computer – Ankit Tiwari Feb 13 '23 at 16:25
  • @AnkitTiwari. So you mean port 19000 in this case? adb reverse tcp:19000 tcp:19000 – mightycode Newton Feb 13 '23 at 16:27
  • Yes, change it to 19000 – Ankit Tiwari Feb 13 '23 at 16:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251834/discussion-between-mightycode-newton-and-ankit-tiwari). – mightycode Newton Feb 13 '23 at 16:32
  • @AnkitTiwari. Oke, I did adb reverse tcp:19000 tcp:19000 and after expo start. And that worked. But if I do now expo start... same error – mightycode Newton Feb 13 '23 at 16:36
  • I think instead of adb you've to look for expo command to connect you computer port inside your emulator. In order to connect your emulator to your computer you've to open ports from both side from your computer as well as your emulator – Ankit Tiwari Feb 13 '23 at 16:38
  • 1
    Oke, I changed the backend to: http://192.168.1.68:8000 and in frontend also: http://192.168.1.68:8000. and it works now :) – mightycode Newton Feb 13 '23 at 17:52
  • @AnkitTiwari. Maybe you have an idea about this issue: https://stackoverflow.com/questions/75445372/calling-property-of-api-conext-error-element-type-is-invalid-expected-a-strin – mightycode Newton Feb 14 '23 at 09:32

1 Answers1

0

Oke, I changed the backend to:

192.168.1.68:8000

and in frontend also:

192.168.1.68:8000

and it works now :)

This was indeed very stupid of me. Because I did fetch('//192.168.1.68:19000'). But that is my emulator host. Of course that doesn't work

mightycode Newton
  • 3,229
  • 3
  • 28
  • 54