nocrox
nocrox

Reputation: 65

How to get access to the Internet from a Go program running on Android?

My program can't access to internet when put in android terminal, but in linux works fine. (wget (busybox) works fine with internet in android terminal)

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")
    }
}

compiling with:

go build main.go    # linux
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build $(bin).go    # android

Upvotes: 1

Views: 407

Answers (2)

Jeremy Wall
Jeremy Wall

Reputation: 25245

Android does not have full support for Go yet. If at some point in the future they add a Go sdk this will be easier. But for right now unless you feel like modifying Go's source yourself or writing your own set of libraries to handle Androids different environment this is going to be difficult to impossible.

Upvotes: 1

Chris Stratton
Chris Stratton

Reputation: 40397

Android does not have an /etc/resolv.conf

Somewhere in your code, or more likely one of those libraries you import, you are assuming a traditional linux userspace, which is not the case on Android.

maybe the "GOOS=linux" is the source of the mistake.

Upvotes: 2

Related Questions