1

I am using rest services to get the response for my react-native android app. Whenever I am using the localhost(i tried with 127.0.0.1) with port 8080 but fetch is giving me [TypeError: Network request failed]. Fetch request is working fine without localhost. Rest service is working fine when used with Postman.

I am running my code using expo. And for backend dependency using Maven.

I tried with both physical device and emulator both failed, checked with port 9090. I tried to change the server address in the application.properties file after which spring-boot backend stopped which is due to server address configuration problem. (Error: APPLICATION FAILED TO START


Description:

The Tomcat connector configured to listen on port 9090 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 9090, or configure this application to listen on another port.)

insertPost() {

return fetch('http://127.0.0.1:9090/getPost/PO397ba3306cc211e98f8249277cd661ec')
    .then(res => res.json())
    .then(json => {
        console.log(json)
    })
    .catch(error => console.error(error));
}

console.log(json) should print the object returned from fetch.

1
  • Has your spring-boot application started to run? As in can you hit your controller via an API call itself? If you have a service running on that port, why not change your spring boot config to deploy on a different port in your application.properties file. server.port=some other port Commented May 2, 2019 at 15:48

3 Answers 3

4

You should use the 'IP address' of your system

http://YourIpAddress:9090/getPost/PO397ba3306cc211e98f8249277cd661ec

Please check your connected device and system in the same network. Please refer below documentation:

https://facebook.github.io/react-native/docs/running-on-device

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

1 Comment

and if you want to use the specefic port you need to reverse it. for example adb reverse tcp:3000 tcp:3000
1

You shouldn't return the fetch function. Try to remove the return word to see if it works.

Comments

0

Try using this,

http://10.0.2.2:portnumber/

10.0.2.2 - Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

4 Comments

how to set this alias
Try using redir add tcp:oldportnumber:newportnumber Refer developer.android.com/studio/run/emulator-networking.html
The following command sets up a redirection that handles all incoming TCP connections to your host (development) machine on 127.0.0.1:5000 and will pass them through to the emulated system on 10.0.2.2:6000 "redir add tcp:5000:6000"
have you tried this before as i tried but still getting errors via this method as it is giving error(console.error: "Getting Error for fetch function", {"line"........."sourceURL":"192.168.43.252:19001/node_modules/expo/…) btw problem already get solved by using the solution provided by Subhash Patel.

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.