react native Fetch Network request failed on android

问题: I'm trying to receive some simple json from mocky. React native fetch function: getMemberDomainList = async (name) => { try { let response = await fetch('https...

问题:

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_modulesreact-nativeLibrariesRendererossReactNativeRenderer-dev.js:2348 TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (...LibrariesRendererossReactNativeRenderer-dev.js:4337)
    at XMLHttpRequest.dispatchEvent (...LibrariesRendererossReactNativeRenderer-dev.js:10760)
    at XMLHttpRequest.setReadyState (...LibrariesRendererossReactNativeRenderer-dev.js:10511)
    at XMLHttpRequest.__didCompleteResponse (...LibrariesRendererossReactNativeRenderer-dev.js:10343)
    at ...LibrariesRendererossReactNativeRenderer-dev.js:10449
    at RCTDeviceEventEmitter.emit (...LibrariesComponentsDrawerAndroidDrawerLayoutAndroid.android.js:11)
    at MessageQueue.__callFunction (...LibrariesARTReactNativeART.js:362)
    at blob:http://localhost:8081/79251787-d190-4650-8040-23d091c08738:2334
    at MessageQueue.__guard (...LibrariesARTReactNativeART.js:312)
    at MessageQueue.callFunctionReturnFlushedQueue (...LibrariesARTReactNativeART.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.


回答1:

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?


回答2:

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.

  • 发表于 2019-03-30 16:55
  • 阅读 ( 4017 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除