我有一个 React Native 应用程序,它正在发出一个简单的登录请求。此请求在 ios 模拟器中有效,并且在我将构建安装到我的设备上时有效。当我从 Play 商店下载应用程序时它不起作用。
我已尝试以下操作: 我已重新安装并安装了该应用程序。查看生产日志,我没有看到请求到达 rails 服务器的任何记录。我一直在使用 bugsnag 日志记录。
这是请求:
sendLoginEmail = (isResend) => {
bugsnag.notify(new Error(`1: ${this.state.email}`))
enterEmail(this.state.email).then(response => {
console.log("sle response!")
console.log(response)
bugsnag.notify(new Error(`2: ${this.state.email}`))
bugsnag.notify(new Error(JSON.stringify(response)))
if (response.status === 200) {
this.setState({isEmail: false, isLoading: false, warningMessage: isResend ? "Email resent" : null});
} else {
this.setState({isLoading: false, warningMessage: "Something went wrong. Please try again."})
}
}).catch(response => {
bugsnag.notify(new Error(JSON.stringify(response)))
})
}
enterEmail
看起来像这样:
export const enterEmail = (email) => {
return fetch(`${host}/v2/users`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: {
email
}
})
})
}
Bugsnag 记录第一个错误 ( bugsnag.notify(new Error(
1: ${this.state.email} ))
) 并捕获抛出的错误并记录响应。该响应显示在index.android.bundle下列行:new TypeError('Network request failed')
。
这是我的 nginx.error.logs:
2019/08/23 09:57:46 [info] 885#885: *246 client closed connection while waiting for request, client: 124.251.44.162, server: 0.0.0.0:80
2019/08/23 10:01:44 [info] 885#885: *265 client 208.94.74.81 closed keepalive connection
2019/08/23 11:15:43 [info] 885#885: *267 client 103.208.200.14 closed keepalive connection
2019/08/23 14:03:38 [info] 885#885: *269 client 151.217.75.156 closed keepalive connection
2019/08/23 15:01:17 [info] 885#885: *271 client closed connection while waiting for request, client: 71.82.97.158, server: 0.0.0.0:80
2019/08/23 15:01:17 [info] 885#885: *272 client 71.82.97.158 closed keepalive connection (104: Connection reset by peer)
2019/08/23 17:18:01 [info] 885#885: *274 client 186.154.93.81 closed keepalive connection
2019/08/23 19:36:42 [info] 885#885: *276 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:37:19 [info] 885#885: *288 client 151.217.75.156 closed keepalive connection
2019/08/23 19:40:22 [info] 885#885: *311 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:42:05 [info] 885#885: *341 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:42:05 [info] 885#885: *339 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:42:05 [info] 885#885: *332 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:42:05 [info] 885#885: *335 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 19:44:52 [info] 885#885: *348 client 47.62.114.44 closed keepalive connection (104: Connection reset by peer)
2019/08/23 20:51:07 [info] 885#885: *352 client 84.41.107.234 closed keepalive connection
2019/08/23 21:23:54 [info] 885#885: *354 client 181.118.94.19 closed keepalive connection
2019/08/23 22:02:16 [info] 885#885: *356 client 138.122.20.90 closed keepalive connection
2019/08/23 22:47:07 [info] 885#885: *358 client sent invalid method while reading client request line, client: 61.219.11.153, server: , request: ""
2019/08/23 22:56:28 [info] 885#885: *359 client 150.242.255.221 closed keepalive connection
我怀疑我的 nginx 配置有问题,但我不知道它是什么。
我猜生产/调试 android 版本在某种程度上是不同的,这可以解释为什么当从 android studio 安装时请求在我的设备上工作,而不是从 Play 商店安装。我不知道这有什么区别。
谢谢。
问题是 Android 现在默认只允许通过 HTTPS 发出请求。我的请求是通过 HTTP 发送的。发生这种情况时,请求会以静默方式失败。
要允许 HTTP 请求,请将以下内容添加到application
您的清单 ( AndroidManifest.xml
):android:usesCleartextTraffic="true"
或者,您可以通过 HTTPS 发出请求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句