merry-go-round
merry-go-round

Reputation: 4625

(Error: Network Error) - Can't make a POST request from android simulator (Wifi is ON!)

I'm making a POST request to my own server and my server doesn't even receive any request (no request was shown in server terminal).

I can use an internet on browser inside of the simulator. But it's giving me the error below.

Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
    at XMLHttpRequest.js:485
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:260)
    at MessageQueue.js:101
    at MessageQueue.__guard (MessageQueue.js:228)

And below is my POST request using axois

export const doAuthLogin = ({ username, password }) => async dispatch => {
  axios.post(`${ROOT_URL}/rest-auth/login/`, {
    username,
    password
  }).then(response => {
      console.log(response);
      // Save Token post is Already await. 
      AsyncStorage.setItem('auth_token', response.token);
      dispatch({ type: AUTH_LOGIN_SUCCESS, payload: response.token });
    })
    .catch(response => {
      console.log(response);
      dispatch({ type: AUTH_LOGIN_FAIL, payload: response.non_field_errors });
    });
};

What is Network Error? And how can I fix it?

Thank you so much!!

Upvotes: 1

Views: 3599

Answers (2)

Hana Alaydrus
Hana Alaydrus

Reputation: 2220

If you put your API in localhost you can do the following steps.

  1. Check your laptop IP by typing ifconfig (for ubuntu and mac) or ipconfig (for windows). For example you got 192.168.40.6

  2. Run your django for examplepython manage.py runserver 192.168.40.6:8000

  3. Specifiy your axios url. For example

    axios.post(http://192.168.40.6:8000/rest-auth/login/, {...})

  4. Make sure your phone connect to the same wifi with your laptop, and you can try the connection in your phone browser by go to http://192.168.40.6:8000

Upvotes: 1

mradziwon
mradziwon

Reputation: 1236

What is your ROOT_URL are you trying to do request to localhost? If it's the case try changing ROOT_URL to 10.0.2.2:YOUR_PORT. Check this link for more information about android emulator and networking.

Upvotes: 3

Related Questions