5

i trying to connect react native android application with Laravel Api. So i using a simple Api to testing whether the request were sent. When using POSTMAN it is working. But when i using emulator from android studio it throw the error of "Network Request Failed". What is the problems?

The API return json tested by POSTMAN:

{"result":"hello world"}

But in react native

enter image description here

This is my function

async onRegisterPressed() {
        let response= await fetch('http://127.0.0.1:8000/api/register',{
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                    name:this.state.name,
                    email:this.state.email,
                    password:this.state.password,
                    password_confirmation:this.state.password_confirmation,

            })
        });

        let res= await response.json();

            console.log(res);


}
kablanfatih
  • 473
  • 4
  • 14
masterhunter
  • 551
  • 2
  • 10
  • 29

1 Answers1

8

I suspect that 127.0.0.1 will point to the emulator itself in your computer where the server is currently running. Try replacing 127.0.0.1 with 10.0.2.2 if you are on AVD or 10.0.3.2 if you are on Genymotion or with your computer's actual IP address.

Mμ.
  • 8,382
  • 3
  • 26
  • 36