我正在试用gomobile,并想在应用程序启动前将一些数据发送到app服务器。我使用的是gomobile中包含的基本示例应用程序模板。我在main的开头添加了代码:
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "X.X.X.X:8000/log", strings.NewReader("TEST"))
client.Do(req)
app.Main(func(a app.App) {
...
}
....
}应用程序在启动时立即崩溃。我确信我在GET请求中使用了正确的IP。
HTTP请求的发出方式有什么问题吗?
(我正在Android上测试)
发布于 2021-02-28 04:01:34
http.NewRequest可能会返回错误,因为您的网址不包含方案(http/https),因此无效。将"X.X.X.X:8000/log"更改为"http://X.X.X.X:8000/log"。
此外,您应该处理错误,即使它只是一个对panic的调用,因为这会告诉您哪里出了问题。
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "http://X.X.X.X:8000/log", strings.NewReader("TEST"))
if err != nil {
panic(err)
}
_, err = client.Do(req)
if err != nil {
panic(err)
}
app.Main(func(a app.App) {
...
}
....
}此外,当在android上测试时,你的恐慌很可能会出现在adb logcat的系统日志中(虽然不是100%确定gomobile应用程序是不是这样)。
https://stackoverflow.com/questions/66402377
复制相似问题