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);


}
10
  • Try to open 127.0.0.0:8000 on your emulator's browser. is it working? Commented Jul 1, 2017 at 9:33
  • TypeError is thrown when a network error is encountered. Can you access the internet in the emulator? Commented Jul 1, 2017 at 9:33
  • @SagarKhatri is not working. Commented Jul 1, 2017 at 9:47
  • @D-reaper Yes the internet is connected in emulator Commented Jul 1, 2017 at 9:48
  • @masterhunter can you make a request to a random website and see if you get a response? Commented Jul 1, 2017 at 9:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Work fine with Computer's actual IP address.
Thanks @Mμ. this was the issue. After 4 hours of try and error you saved my day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.