I’m trying to get data from an API to Android, but something happened that prevents me from getting the data or results in a timeout error. I have tested this code on the following platforms:
-
Snack Expo (Web): It passed and I was able to get the data successfully.
-
Expo (localhost: web): It passed and I was able to get the data successfully.
-
Expo (Android Emulator): It failed, as it took a very long time to load and no data was received.
-
Expo GO (real Device): It failed with the error message "Uncaught Error: java.net.ConnectException: Failed to connect to /ip".
Version
-
"expo": "~48.0.15",
-
"react": "18.2.0",
-
"react-native": "0.71.8",
-
-
This is my code, App.json is folowing
App:
import React, { useEffect, useState } from 'react'; import { View, Text, FlatList } from 'react-native'; export default function Test2() { const [data, setData] = useState([]); useEffect(() => { fetchData(); }, []); const fetchData = () => { console.log("start"); fetch('https://646ee64209ff19b120864a07.mockapi.io/users') .then(response => response.json()) .then(data => { console.log("data:", data); setData(data); }) .catch(error => { console.error(error); }) .finally(() => { console.log("end"); }); }; return ( <View> <FlatList data={data} renderItem={({ item }) => ( <View> <Text>Id user: {item.id}</Text> <Text>Name user: {item.name}</Text> <Text>-------------------------</Text> </View> )} keyExtractor={item => item.id} /> </View> ); }
App.Json
{
"expo": {
"name": "MyApp",
"slug": "MyApp",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}
I Tried:
-
Check Internet: good connect
-
Check JSON file: pass
link of JSON I used for test: https://646ee64209ff19b120864a07.mockapi.io/users
2
Answers
After 30 mins, I find the solution. Because Expo don't have Manifest file, we will do it in another way Add this code to app.json:
//Skip any part if it already exist on your file
Try adding this in your androidManifest.xml file