反应本机获取抛出异常网络请求在android上失败

反应本机获取抛出异常网络请求在android上失败,android,reactjs,react-native,Android,Reactjs,React Native,我正在对API调用使用fetch并获取JSON响应。 但它在android上抛出了一个例外,但在iOS上工作 fetch(url, { method: requestMethod, body: formData, headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }) .then((response) => { r

我正在对API调用使用fetch并获取JSON响应。
但它在android上抛出了一个例外,但在iOS上工作

fetch(url, {
    method: requestMethod,
    body: formData,
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
})
.then((response) => {
    return response.json();
})
.then((responseJson) => {
    console.log("Response JSON :- " + JSON.stringify(responseJson));
})
.catch((error) => {
    console.log("Exception :- " + error);
})
错误:-

“类型错误:网络请求失败。
位于XMLHttpRequest.xhr.onerror(blob:)


使用服务器的IP地址,而不是url中的本地主机:8081。

使用服务器的IP地址,而不是url中的本地主机:8081。

您不需要将:8080与IP一起添加,只需使用设备IP/服务路径。删除“标题:{Accept:'application/json',Content Type':'application/json',}后,问题解决您不需要添加:8080和ip,只需使用设备ip/服务路径。删除“标题:{Accept:'application/json','Content Type':'application/json',}”后,问题解决
at XMLHttpRequest.dispatchEvent (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:28784:27).

at XMLHttpRequest.setReadyState (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:28537:20).

at XMLHttpRequest.__didCompleteResponse (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:28364:16).

at blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:28474:47.

at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:3280:37).

at MessageQueue.__callFunction (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:2577:44).

at blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:2334:17.

at MessageQueue.__guard (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:2531:13).

at MessageQueue.callFunctionReturnFlushedQueue (blob:http://localhost:8081/1a80ef13-8ce9-4c75-a477-e214c5fd3ff2:2333:14)".