2

I am unable to make a http request from a Go fyne application running on android, below is a simple example to show the issue

package main

import (

     "io/ioutil"
     "net/http"
     "log"
 
    "fyne.io/fyne"
    "fyne.io/fyne/app"
    "fyne.io/fyne/widget"
 
)


func main() {

    app := app.New()
    w := app.NewWindow("Android http issue")
    w.SetContent(widget.NewVBox(
        widget.NewLabel("Run test"),
        widget.NewButton("Connect", func() {
             go func() {
                HttpGetData(w)
             }()
        
        }),
    ))
    w.ShowAndRun()
} 
 

func HttpGetData( win fyne.Window) {
    resp, err := http.Get("http://date.jsontest.com/" )
    if err != nil {
        log.Println("%v", err)
    }
    defer resp.Body.Close()
    bodyBytes, _ := ioutil.ReadAll(resp.Body)
    StartScreen(win,  string(bodyBytes))

}

func StartScreen(win fyne.Window, data string) {
    l := widget.NewLabel("data" +data)
    win.SetContent(l)

}

I can run the code on linux with go run -tags mobile . -t
first screen
entry screen running on linux

then I can fire the event to make a http get request to the remote server and see the http response in the gui
enter image description here

as we can see everything works via go run -tags mobile . -t on linux

now I package as an apk using fyne fyne package -os android -appID basic.client -icon ico.png
install with adb adb install <path to apk>/basicExample.apk

When I run the app in android I get to the first screen, then fire the event as before.
The http request is never fired, I only get a crytpic error in logcat

F/libc ( 4711): Fatal signal 6 (SIGABRT), code -6 in tid 4739 (basic.client)
E/InputDispatcher( 535): channel '344d7ddf basic.client/org.golang.app.GoNativeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

any help would be greatly appreciated

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="basic.client"
        android:versionCode="1"
        android:versionName="1.0">

        <application android:label="Client" android:debuggable="true">

        <uses-permission android:name="android.permission.INTERNET" />

        <activity android:name="org.golang.app.GoNativeActivity"
                android:label="Client"
                android:configChanges="orientation|keyboardHidden">
                <meta-data android:name="android.app.lib_name" android:value="client" />
                <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
        </application>
</manifest>

UPDATE from comment
android version 28
golang version go1.14.4 linux/amd64
fyne version fyne.io/fyne v1.3.0

Update 2
Added following attributes to the application tag in the manifest
android:usesCleartextTraffic="true" tools:targetApi="28"

Results in following error for the fyne package command
failed to find table ref by "attr/usesCleartextTraffic"

I added a print statement to the fyne package command to log the build environment variables, this is what they look like

GOOS=android   
GOARCH=arm   
CC={path to}/ndk/21.2.6472646/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi16-clang   
CXX={path to}/ndk/21.2.6472646/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi16-clang++   
CGO_ENABLED=1   
GOARM=7  
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nigel Savage
  • 991
  • 13
  • 26
  • maybe you should include your various go, fyne, android versions. –  Jun 22 '20 at 16:54
  • thanks mh-cbon good point updated question – Nigel Savage Jun 22 '20 at 17:08
  • It *might* be related that [Android blocks HTTP traffic](https://stackoverflow.com/q/45940861) (or [alternative](https://stackoverflow.com/q/51902629)), but the error message is quite cryptic. – xarantolus Jun 22 '20 at 20:13
  • thanks xarantolus for the link will try the clear-text workarounds and update with the result – Nigel Savage Jun 22 '20 at 21:15
  • You are continuing on error as well, so it could be that the crash is after the call perhaps? Is the server getting the request? – andy.xyz Jun 22 '20 at 22:29
  • From the server logs the request is never made, I have tried various permutations but always the code fails on resp, err := http.Get("http://date.jsontest.com/" ) and the only logcat error as posted, err I believe is nil – Nigel Savage Jun 23 '20 at 09:07
  • Can you try printing the value of the err and body to verify that one of them is not ni? – andy.xyz Jun 24 '20 at 11:41
  • It looks like the usesCleartextTraffic tag is not supported by fyne package at this time, should have been addressed in 1.3.0 but will get added now you point it out – andy.xyz Jun 24 '20 at 11:41
  • @andy.xyz definitely will try both as u suggest – Nigel Savage Jun 24 '20 at 16:10

1 Answers1

1

With the latest version of Fyne we have fixed some issues with the packager and Android manifest. Internet permission is not enabled by default, but if you add it under the storage permission definitions it should now work.

We are working on better permission request code that will work cross platform, should be published later this year.

andy.xyz
  • 2,567
  • 1
  • 13
  • 18