18

This is long-standing problem for me and I can't find solution at all.

Within my react native project(react-native-cli), I send request to server using axios package. But when I'm testing with my android phone, it gives [Error: Network Error]. With emulator, it's okay.

I'm sure this is not back-end problem because I've tested with several servers. And I also used fetch instead but got same error.

Here is version information.

"axios": "^0.19.0",
"react": "16.9.0",
"react-native": "0.61.5",

Sometimes, it's working on android phone too. This problem is biggest mystery and I suffered for a long time.

Somebody who experienced this issue, please alive me. Thank you for careful reading.

Sometimes, it's not working even with android emulator. But after deleted original emulator and created new emulator, it's working.

const serverUrl = "https://server.com";
axios.post(serverUrl + "/api/candidates/login", {
            "email": this.state.email ,
            "password": this.state.password
        }, {
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((response) => {
                console.log(response.data);
        }).catch((error) => {
                console.log(error);
        })
10
  • This error is with "axios": "^0.19.0". If you downgrade your axios version for ex : "axios": "^0.18.0" then run npm install and then if you run your app it will run successfuly. Commented Jan 6, 2020 at 4:54
  • I don't know proper solution but this is workaround. Commented Jan 6, 2020 at 4:54
  • Not helpful. I downgraded axios but still same error. I think this maybe sth like cache problem. Commented Jan 6, 2020 at 13:58
  • @AndrewTerex did you find any solution? Commented Jan 13, 2020 at 12:39
  • Nope, I'm going to return to expo. Commented Jan 14, 2020 at 2:53

10 Answers 10

15

I think this is a problem with http and https request. SO to enable https request try the one below :

The easy way to implement this is to use this attribute to your AndroidManifest.xml where you allow all http for all requests:

android:usesCleartextTraffic="true"
Sign up to request clarification or add additional context in comments.

4 Comments

I already added it to 'AndroidManifest.xml' in the application tag.
after adding it to AndroidManifest did you do android:bundle and then build a new apk and tried with that?
I'm testing on development server with my android phone.
what will be the impact of adding this option in versions prior to android 9 ?
9

try adding bellow code in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Comments

8

For Android, instead of using: localhost, use 10.0.2.2.

Comments

6

Was facing the same issue & solved it by doing:

1- Find the IP Address of your machine.

2- Replace your localhost address with your IP address**

 replace ==>  'http://localhost:8000/api/download'

 with ==> 'http://192.120.16.108:8000/api/download'

Comments

1

I have tried adding access network state permission and usesCleartextTraffic and both of the solutions didn't help. I am still getting this error.Out of all my endpoints (30+), I am only getting this error while sending a post request to logout with a refresh token as parameter. The rest of the api is working fine.

I have found the problem. The endpoint returns 205 and this creates an error returning Network Error instead of success. I am thinking to tell my backend to change the response status code to 200.

Comments

1

My problem was that I was using the host url as http://127.0.0.1 while running my app on an emulator.

With emulators, the use of "localhost" OR "127.0.0.1" does not apply since emulator tries only within its scope.

If you are running your app in a local env, make sure that both your device and the server are in the same network and use the IP address of the server as the host url.

In my case it was http://192.168.8.101:8080

Comments

1

facing same issue & solved it by doing:

1- Make sure your PC and android device(if external) are connected to the same wifi network 2- Find the IP Address of your machine|| setting=>wifi=>select the wifi you are connected to => copy the IPV4 address and use it instead of localhost. 3- Replace your localhost address with your IP address**

replace ==> 'http://localhost:8000/register

with ==> 'http://192.120.16.108:8000/register

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I face the same issue. I resolve it by making some changes in my backend project with ASP.NET core 6. I had two urls for lunching app in backend: "applicationUrl": "https://localhost:7218,http://localhost:5218" and i changed it to "applicationUrl":"http://localhost:5218". In my react native app i maked backend host to http://10.0.2.2:5218 and it was working for me.

1 Comment

Hi, consider using the tilda sign ( ` ) to wrap around the code parts within your text so that it's more readable.
0

If you're targeting API level 28 or above, and using HTTP (not HTTPS), you may need to add a network security configuration.

Create a file at android/app/src/main/res/xml/network_security_config.xml with the following content:

`<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>`

Then reference it in AndroidManifest.xml:

<application
android:networkSecurityConfig="@xml/network_security_config"
...>
<!-- ... -->

Comments

-1

I faced this same problem when I used a localhost but I was able to solve this problem when I used Expo ip http://192.168.1.8:8000

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.