我的程序在 android 终端中无法访问互联网,但在 linux 中工作正常。(wget(busybox)在 android 终端中与互联网正常工作)
package main
import (
"io"
"io/ioutil"
"net/http"
)
func Url(url string)(string, io.ReadCloser, http.Header, error){
var c = http.Client{}
inf, err := c.Get(url)
if err == nil {
data,_ := ioutil.ReadAll(inf.Body)
return string(data), inf.Body, inf.Header, err
}
return "", nil, nil, err
}
func main() {
print("test internet... ")
c,_,_,err := Url("http://ifconfig.me/ip")
if err == nil {
println("\n ip:", c)
}else{
println("error")
}
}
编译:
go build main.go # linux
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build $(bin).go # android