skip to Main Content

Recently I have been facing "Network request failed" issue with the React Native Android app on physical devices. Strangely it works fine on the Emulator but fails only on physical devices be it Android 7, 8, etc. I have upgraded Android Studio to latest, React Native to latest, but still getting this issue.

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log("MYJSONRESPONSE", json))
  .catch(e => console.log("ERRORRESP", e))

Above is a simple API call, even if I put any other working API URL it will still show the same Network error. Some of the other sample APIs I tried https://dummyapi.io/data/v1/user?limit=10 https://reqres.in/api/users?page=2

[TypeError: Network request failed]

This happens on a fresh react native project as well even on 0.62 or 0.65

 "dependencies": {
    "react": "17.0.2",
    "react-native": "0.65.1"
  }

Anybody with any solution for this issue?

2

Answers


  1. Chosen as BEST ANSWER

    the issue has been resolved and the solution was,

    android:usesCleartextTraffic (adding this line in android Manifest file)
    

    (optionally) setting up network security group for the system/custom domains.

    https://developer.android.com/training/articles/security-config
    

    Important note: When testing on physical devices or even emulators, please make sure you are connected to a working Internet connection. I struggled for a few hours just because my Internet was not working properly and the "Network request failed" error in that case was coming due to "No Internet". Everything was working fine once the Internet was up again.

    Thank you everyone for your suggestions.


  2. It might work if you add a www, like https://www.jsonplaceholder.typicode.com/todos/1

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