首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在可穿戴应用程序上下载文件

在可穿戴应用程序上下载文件
EN

Stack Overflow用户
提问于 2015-08-06 14:49:28
回答 1查看 142关注 0票数 0

我在我的Moto360智能手表上创建了一个小应用程序来播放从web服务器下载的音频内容。我使用下面的代码片段从给定的URL下载内容。

代码语言:javascript
复制
@Override
protected String doInBackground(String... sUrl) {
    Log.d(TAG, "Downloading file !!!");
    InputStream input = null;
    OutputStream output = null;
    HttpURLConnection connection = null;
    try {
        URL url = new URL(sUrl[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        // expect HTTP 200 OK, so we don't mistakenly save error report
        // instead of the file
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.e(TAG, "Error on http connection !! ");
            return "Server returned HTTP " + connection.getResponseCode()
                    + " " + connection.getResponseMessage();
        }

        // this will be useful to display download percentage
        // might be -1: server did not report the length
        int fileLength = connection.getContentLength();

        // download the file
        input = connection.getInputStream();
        output = new FileOutputStream("/sdcard/file_name.extension");

        byte data[] = new byte[4096];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            // allow canceling with back button
            if (isCancelled()) {
                input.close();
                return null;
            }
            total += count;
            // publishing the progress....
            if (fileLength > 0) // only if total length is known
                publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }
    } catch (Exception e) {
        Log.e(TAG, "exception : " + e);
        return e.toString();
    } finally {
        try {
            if (output != null) {
                Log.d(TAG, "download finished!");
                output.close();
            }
            if (input != null)
                input.close();
        } catch (IOException ignored) {
        }

        if (connection != null)
            connection.disconnect();
    }
    return null;
}

但我有一个超时连接:

代码语言:javascript
复制
08-06 16:22:06.345  17925-17941/com.example.ile14017.moto360 E/DownloadTask﹕ exception : java.net.ConnectException: failed to connect to api.europe1.fr/85.116.42.56 (port 80): connect failed: ETIMEDOUT (Connection timed out)

我试图更改连接超时值(setConnectTimeout方法),但结果是相同的。下载的文件大小约为2Mo。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-06 15:32:51

你的手表(通过BT)和你的手机相连吗?如果是这样的话,那么即使在手表上设置wifi,您的手表也无法使用URLConnection进行网络呼叫。您需要考虑这种情况,并使用您的手机为您做下载,然后转移文件到您的手表使用,例如,ChannelApi。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31858942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档