英文:
Cannot get data from API for android device (React-Native)
问题
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 following:
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
英文:
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
答案1
得分: 1
"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:
\"android\": {
\"config\": {
\"cleartextTraffic\": true
},}"
//Skip any part if it already exists in your file
英文:
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:
"android": {
"config": {
"cleartextTraffic": true
},}
//Skip any part if it already exist on your file
答案2
得分: 0
尝试将以下内容添加到您的 androidManifest.xml 文件中:
<application
...
android:usesCleartextTraffic="true">
英文:
Try adding this in your androidManifest.xml file
<application
...
android:usesCleartextTraffic="true">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论