我正在尝试执行 POST http 请求来获取应用程序中的用户令牌。 如果使用 WEB 版本(相同的代码)和 http 客户端(Bruno),它就可以工作。
我无法显示真实的 API 地址,但类似于
http://example.org.br:3550/
我已经将 `usesCleartextTraffic` 添加到我的 app.json
[
"expo-build-properties",
{
"android": {
"usesCleartextTraffic": true
}
}
],
我的登录功能:
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)
}
})
}
我得到了什么:
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
}
}
由于某种原因AndroidWifi无法正常工作。一些 DNS 问题。尝试使用 8.8.8.8 DNS 设置静态 IP。更改我自己电脑的DNS。什么都没起作用。 使用模拟器“LTE”连接使其工作。
希望它对某人有帮助。