在可穿戴应用程序上下载文件
Download file on a wearable app
我在我的 Moto360 smartwtach 上创建了一个小应用程序来播放从网络服务器下载的音频内容。我使用以下代码片段从给定的 URL.
下载内容
@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;
}
但是我连接超时:
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
您的手表是否已(通过 BT)连接到您的 phone?如果是这样,那么即使在手表上设置了 wifi,您的手表也没有 wifi 连接来使用 URLConnection 进行网络呼叫。您需要考虑这种情况并使用您的 phone 为您下载,然后使用 ChannelApi 将文件传输到您的手表。
我在我的 Moto360 smartwtach 上创建了一个小应用程序来播放从网络服务器下载的音频内容。我使用以下代码片段从给定的 URL.
下载内容@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;
}
但是我连接超时:
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
您的手表是否已(通过 BT)连接到您的 phone?如果是这样,那么即使在手表上设置了 wifi,您的手表也没有 wifi 连接来使用 URLConnection 进行网络呼叫。您需要考虑这种情况并使用您的 phone 为您下载,然后使用 ChannelApi 将文件传输到您的手表。