React-Native - can't fetch from connected Android device to localhost endpoint
First try this
- For android: run the following command in terminal and try again
adb reverse tcp:8081 tcp:8081
If it doesn't work try this
Shake device(android)
Select dev settings and click "Debug server host & port for device"
- Under debugging type local ip address and port number. (Ex: http://127.0.0.1:8080)
React native fetch api does not work with localhost, IP, or even 10.0.2.2
This may be an issue with the django configuration. Especially if your phone's web browser is unable to get the expected response as well.
From the Django docs:
Note that the default IP address, 127.0.0.1, is not accessible from other machines
on your network. To make your development server viewable to other machines on the
network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).
See How to access the local Django webserver from outside world, though the answer also applies on a local network:
python manage.py runserver 0.0.0.0:8000
You can replace 0.0.0.0
with the machine's address if you know it.
Also worth noting in your response handler that response.json()
returns a Promise, not JSON. Instead you want:
fetch('http://my-ip-address:8000/restapi/')
.then(response => response.json())
.then(responseJson => {
console.log("success");
console.log(responseJson);
})
.catch(error => {
console.error(error);
})
Related Topics
Error: "Adb Connection Error:An Existing Connection Was Forcibly Closed by the Remote Host"
How to Restrict Android App to Specific Device Make
How to Convert Image into Byte Array and Byte Array to Base64 String in Android
Android: Clear Cache of All Apps
How to Read the File Content from the Internal Storage - Android App
Error Referencing an Inner Class View in Layout/Main.Xml
How to Use Accelerometer to Measure Distance for Android Application Development
Exclude a Class from the Build in Android Studio
Losing Data When Rotate Screen
Reading a Specific File from Sdcard in Android
Edittext Loses Content on Scroll in Listview
Detect the Status of Two Sim Cards in a Dual-Sim Android Phone
Android Calendarview for Showing Events
Installation Error: Install_Parse_Failed_Manifest_Malformed
High Resolution Image - Outofmemoryerror
Understanding Command Through Adb Shell and Through Code - Android