16

I'm trying to receive some simple json from mocky.

React native fetch function:

getMemberDomainList = async (name) => {
  try {
    let response = await fetch('https://5c9cc9ed3be4e30014a7d287.mockapi.io/api/domain', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    });
    let responseJson = await response.json();
    return responseJson;
  } catch (error) {
    console.error(error);
  }
}

I have tested the address in chrome on windows, it returns the expected mock data. But when the function is called on my android phone I get this error enter image description here

Error from remote debugger

...\node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:2348 TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (...\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:4337)
    at XMLHttpRequest.dispatchEvent (...\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:10760)
    at XMLHttpRequest.setReadyState (...\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:10511)
    at XMLHttpRequest.__didCompleteResponse (...\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:10343)
    at ...\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:10449
    at RCTDeviceEventEmitter.emit (...\Libraries\Components\DrawerAndroid\DrawerLayoutAndroid.android.js:11)
    at MessageQueue.__callFunction (...\Libraries\ART\ReactNativeART.js:362)
    at blob:http://localhost:8081/79251787-d190-4650-8040-23d091c08738:2334
    at MessageQueue.__guard (...\Libraries\ART\ReactNativeART.js:312)
    at MessageQueue.callFunctionReturnFlushedQueue (...\Libraries\ART\ReactNativeART.js:139)

I'm also running a WebView in my app, which is pointing to a web url, it loads perfectly so I am sure that the phone has internet permission and access etc.

Rasmus Puls
  • 3,009
  • 7
  • 21
  • 58

4 Answers4

4

In the latest android versions http requests are not allowed by default. Take a look at this post for further information about allowing http request: How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

sebbab
  • 847
  • 8
  • 13
1

Can't get mockapi.io to work. But the error is not persistent when using services such as:

https://jsonplaceholder.typicode.com/todos/1

Might not be an android or react native related problem after all. Also had issues with mockapi.io in postman, though it works fine in chrome.

Rasmus Puls
  • 3,009
  • 7
  • 21
  • 58
0

If you are using emulator then check whether internet is working or not in this emulation using browser. If not check this: Android emulator not able to access the internet

I have also face this problem in emulator but when I generate a release app and install in a real device(andoid 9) then it works fine.

Md. Robi Ullah
  • 1,703
  • 3
  • 20
  • 32
  • 1
    Yes dev/prod build can have effect on things such as internet access or which protocols are allowed. In my case I was able to fetch against https://jsonplaceholder.typicode.com just fine ruling out the idea that I wasn't able to connect to the internet on the emulator. – Rasmus Puls Oct 09 '19 at 08:21
0

Please check backend response status.

If backend is sending contents using 205 status - 205 RESET CONTENT

Android system recognize it as an error - HTTP 205 had non-zero Content-Length: 25.

You can check the status code on postman. So, in this case, the error should be fixed on backend. It should send contents with 200 status code.