12

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);
        })
Andrew Terex
  • 301
  • 1
  • 3
  • 10

4 Answers4

7

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"
Gaurav Roy
  • 8,042
  • 3
  • 17
  • 38
5

try adding bellow code in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Avinash
  • 561
  • 1
  • 7
  • 20
3

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

CodeFinity
  • 898
  • 2
  • 15
  • 17
0

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.