skip to Main Content

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:

  1. Snack Expo (Web): It passed and I was able to get the data successfully.

  2. Expo (localhost: web): It passed and I was able to get the data successfully.

  3. Expo (Android Emulator): It failed, as it took a very long time to load and no data was received.

  4. 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


  1. Chosen as BEST ANSWER

    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. Try adding this in your androidManifest.xml file

    <application
        ...
        android:usesCleartextTraffic="true">
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search