【问题标题】:How to make a http request for a native fyne app on android如何在 android 上对原生 fyne 应用发出 http 请求
【发布时间】:2020-10-12 06:28:04
【问题描述】:

我无法从运行在 android 上的 Go fyne 应用程序发出 http 请求,下面是一个简单的示例来说明问题

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)

}

我可以使用go run -tags mobile . -t在linux上运行代码
第一个屏幕

然后我可以触发事件向远程服务器发出 http get 请求并在 gui 中查看 http 响应

正如我们所见,一切都通过go run -tags mobile . -t 在 linux 上运行

现在我使用 fyne 打包为 apk fyne package -os android -appID basic.client -icon ico.png
使用 adb adb install <path to apk>/basicExample.apk 安装

当我在 android 中运行应用程序时,我会进入第一个屏幕,然后像以前一样触发事件。
http 请求永远不会被触发,我只在 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!

任何帮助将不胜感激

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>

评论更新
安卓版本 28
golang 版本 go1.14.4 linux/amd64
fyne 版本 fyne.io/fyne v1.3.0

更新 2
向清单中的应用程序标签添加了以下属性
android:usesCleartextTraffic="true" tools:targetApi="28"

导致fyne package 命令出现以下错误
failed to find table ref by "attr/usesCleartextTraffic"

我在fyne package 命令中添加了一条打印语句来记录构建环境变量,这就是它们的样子

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  

【问题讨论】:

  • 也许你应该包括你的各种 go、fyne、android 版本。
  • 感谢 mh-cbon 好点更新问题
  • 可能Android blocks HTTP traffic(或alternative)有关,但错误消息非常神秘。
  • 感谢 xarantolus 的链接将尝试明文解决方法并更新结果
  • 您也在继续出错,所以崩溃可能是在通话之后?服务器收到请求了吗?

标签: android go gomobile fyne


【解决方案1】:

在最新版本的 Fyne 中,我们修复了打包程序和 Android 清单的一些问题。默认情况下不启用 Internet 权限,但如果您在存储权限定义下添加它,它现在应该可以工作了。

我们正在开发可以跨平台运行的更好的权限请求代码,应该会在今年晚些时候发布。

【讨论】:

    猜你喜欢
    • 2010-10-15
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多