Search code examples
androidreact-nativeexpohosthostname

Unable to resolve host \"example.org.br\": No address associated with hostname. Expo + React-native + Android


I'm trying to do a POST http request to get the user token in my application. It WORKS if using the WEB version (same code), and in the http client (Bruno).

I can't show the real API address, but is something like http://example.org.br:3550/ I already added `usesCleartextTraffic` to my app.json

      [
        "expo-build-properties",
        {
          "android": {
            "usesCleartextTraffic": true
          }
        }
      ],

my login function:

export async function login(userName: string, password: string) {
  console.log('username:', userName)
  await apiV1
    .post('/account/login', {
      userName,
      password,
    })
    .then(function (response) {
      console.log(response)
      return response
    })
    .catch(function (error) {
      console.log(error)
      if (error.response) {
        console.log('error data: ', error.response.data)
        console.log(error.response.status)
        console.log(error.response.headers)
      } else if (error.request) {
        console.log(JSON.stringify(error.request, null, 2))
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message)
      }
    })
}

what I'm getting:

LOG  [AxiosError: Network Error]
LOG  {
  "UNSENT": 0,
  "OPENED": 1,
  "HEADERS_RECEIVED": 2,
  "LOADING": 3,
  "DONE": 4,
  "readyState": 4,
  "status": 0,
  "timeout": 2000,
  "withCredentials": true,
  "upload": {},
  "_requestId": null,
  "_subscriptions": [],
  "_aborted": false,
  "_hasError": true,
  "_headers": {
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json"
  },
  "_lowerCaseResponseHeaders": {},
  "_method": "POST",
  "_perfKey": "network_XMLHttpRequest_http://example.org.br:3550/v1/account/login",
  "_responseType": "",
  "_response": "Unable to resolve host \"example.org.br\": No address associated with hostname",
  "_sent": true,
  "_url": "http://example.org.br:3550/v1/account/login",
  "_timedOut": false,
  "_trackingName": "unknown",
  "_incrementalEvents": false,
  "_performanceLogger": {
    "_timespans": {
      "network_XMLHttpRequest_http://example.org.br:3550/v1/account/login": {
        "startTime": 301157564.214717
      }
    },
    "_extras": {},
    "_points": {
      "initializeCore_start": 301155072.756717,
      "initializeCore_end": 301155229.869617
    },
    "_pointExtras": {},
    "_closed": false
  }
}

Solution

  • for some reason the AndroidWifi can't work properly. Some DNS issue. Tried setting static IP with 8.8.8.8 DNS. Changing my own computer DNS. Nothing worked. Using the emulator "LTE" connection made it work.

    Hope it helps someone.