react-nativeflightphp

React Native Fetch Return Network Request Failed


I am developing a simple app using React Native. I am testing it on Genymotion Android Emulator. I have created local web server to listen to the requests, it is running at http://localhost:8082/API/. I have tested the api and is working right. Then I make a fetch request from index.android.js.

here's the API sample request from the React Native code :

var api = { getUser(){
        var url = "http://127.0.0.1:8082/API/";
        return fetch(url)
            .then((res) => res.json())
            .catch(
                (error)=>{
                    console.log('error' + error.message);
                    throw error;
                }
            );
    }
}
module.exports = api;

here's the code from Api Server (built with flightPHP)

Flight::route('GET /',function(){
try{
$db = new PDO('mysql:host=localhost;port=3307;dbname=testapp', 'root','');
    $stmt = $db->prepare("SELECT * FROM user LIMIT 1");
    $stmt->execute();
    header('Content-type: application/json');
    echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));

    $db = null;

}catch(Pdoexception $e){
    echo $e->getMessage();
}
});

after execute this call i receive Network Request Failed(). it seems android simulator not recognized the api url. any suggestion ? thanks before


Solution

  • i already solved. just change the API url var url = "http://127.0.0.1:8082/API/ in the React Code. to var url = "http://local-ip-address:8082/API/ to check your local ip just run ipconfig from command line / cmd