gomobile回调(将实时下载的内容转发到Android)?

huangapple go评论106阅读模式
英文:

gomobile callback (forward realtime downloaded content to android)?

问题

我正在学习如何使用gomobile开发Android SDK,这是我的使用案例:

SDK将处理文件下载,并将实时接收到的内容发送给Android,如何实现这一点?

我尝试了类似这样的方法,返回一个ReadCloser,然后Android将从这个流中读取:

func DownloadFile(url string) (io.ReadCloser, error) {
    // 下载文件
    resp, err := http.Get(url)
    if err != nil {
        return nil, err
    }

    if resp.StatusCode != http.StatusOK {
        err = errors.New(fmt.Sprintf("请求URL [%s] 错误 [%s]!", url, resp.Status))
        return nil, err
    }

    return resp.Body, nil
}

然而,从编译后的Java类中,甚至没有这个方法,为什么?

我尝试返回一个通道,但结果是一样的,目标Java类中甚至没有编译。

有没有更好的方法来实现这个?gomobile是否支持回调(Go SDK调用由Android注册的回调函数)?我几乎找不到关于回调使用的任何文档。

英文:

I am studying on how to develop android sdk with gomobile, here is my use case:

The sdk will handle the file download and it will send its realtime received content to andoird, how could this be done?

I tried something like this, return a ReadCloser, then android will read from this stream :

func DownloadFile(url string) (io.ReadCloser, error) {
	// download the file
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		err = errors.New(fmt.Sprintf("requesrting url [%s] error [%s]!", url, resp.Status))
		return nil, err

	}

	return resp.Body, nil
}

however from the compiled java class, there isn't even this method, why?

And I tried to return a channel, but it is the same result, not event compiled in target java class.

Is there any better way to do that? does gomobile supports callback (go sdk call this callback registered by android)? I can hardly find any documentation on that callback usage.

答案1

得分: 6

经过一些搜索,我找到了一些有用的信息,这是一个很好的演示文稿。

https://talks.madriguera.me/2016/gomobile.slide#17

最后,我让我的代码工作了:

// 定义回调接口
type Downloader interface {
Forward(content []byte)
}

// 回调实例
var downloader Downloader

// 保存回调实例
func RegisterCallback(c Downloader) {
downloader = c
}

func Download(url string) error {
// 下载文件
resp, err := http.Get(url)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
    err = errors.New(fmt.Sprintf("请求URL [%s] 错误 [%s]!", url, resp.Status))
    return err
}

defer resp.Body.Close()
buf := make([]byte, 256)
copyBuffer(resp.Body, buf)
return nil

}

func copyBuffer(src io.Reader, buf []byte) (err error) {
for {
nr, er := src.Read(buf)
if nr > 0 {
content := buf[0:nr]
// 回调
downloader.Forward(content)
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return nil
}

在Android中,我只需实现forward接口,然后它就可以正常工作:

// 记得先调用registerCallback()

@Override
public void forward(byte[] bytes) {
System.out.println("--------------");
System.out.println(new String(bytes));
}

英文:

After some googling, I found some useful information, here is a good presentation.

https://talks.madriguera.me/2016/gomobile.slide#17

Finnally, I made my code work:

// define call back interface
type Downloader interface {
	Forward(content []byte)
}

// callback instance
var downloader Downloader

// save the callback instance
func RegisterCallback(c Downloader) {
	downloader = c
}

func Download(url string) error {
	// download the file
	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	if resp.StatusCode != http.StatusOK {
		err = errors.New(fmt.Sprintf("requesrting url [%s] error [%s]!", url, resp.Status))
		return err
	}

	defer resp.Body.Close()
	buf := make([]byte, 256)
	copyBuffer(resp.Body, buf)
	return nil
}

func copyBuffer(src io.Reader, buf []byte) (err error) {
	for {
		nr, er := src.Read(buf)
		if nr > 0 {
			content := buf[0:nr]
			// callback
			downloader.Forward(content)
		}
		if er == io.EOF {
			break
		}
		if er != nil {
			err = er
			break
		}
	}
	return nil
}

In Android, I just implement forward interface, then it works well:

// remember call registerCallback() first

@Override
public void forward(byte[] bytes) {
    System.out.println("--------------");
    System.out.println(new String(bytes));

}

huangapple
  • 本文由 发表于 2017年1月12日 15:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/41607634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定