我正在开发一款应用程序,在通过oAuth 2进行身份验证后,将通过Rails Heroku服务器下载存储在Amazon S3上的zip文件。这是流程:
此时,我希望DownloadManager只关注重定向并从S3获取zip文件,但它失败了。有什么方法可以解决这个问题吗?或者它只是DownloadManager的限制?
我是Android的新手,但仍然没有完全掌握最好的调试方法,所以我没有很多输出来向你展示。但是,似乎DownloadManager.COLUMN_STATUS == DownloadManager.STATUS_FAILED
和DownloadManager.COLUMN_REASON
正在返回“占位符”!
编辑 - 这是我正在使用的代码。编辑隐藏客户等...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("ChapterListActivity", "Item clicked: " + id);
final DownloadManager downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://myapphere.herokuapp.com/api/v1/volumes/2.zip");
DownloadManager.Request request = new Request(uri);
String accessToken = getSharedPreferences("keyhere", MODE_PRIVATE).getString("access_token", null);
Log.i("SLEChapterListActivity", "Getting file with access token... " + accessToken);
request.addRequestHeader("Authorization", "Bearer " + accessToken);
long reference = downloadManager.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long downloadReference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Log.i("ChapterListActivity", "Download completed");
Query query = new Query();
query.setFilterById(downloadReference);
Cursor cur = downloadManager.query(query);
if (cur.moveToFirst()) {
int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) {
String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File mFile = new File(Uri.parse(uriString).getPath());
} else if (DownloadManager.STATUS_FAILED == cur.getInt(columnIndex)){
String statusResult = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_REASON));
Toast.makeText(context, "FAILED " + statusResult, Toast.LENGTH_SHORT).show();
} else if (DownloadManager.ERROR_TOO_MANY_REDIRECTS == cur.getInt(columnIndex)){
String statusResult = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_REASON));
Toast.makeText(context, "TOO MANY REDIRS " + statusResult, Toast.LENGTH_SHORT).show();
}
}
}
};
registerReceiver(receiver, filter);
}
答案 0 :(得分:4)
我在Download Manager sources(第500行)找到了:
3xx:重定向(下载管理器未使用)
它尚不支持。
在我目前的项目中,下载分两步进行:
如果您不喜欢这两个步骤,我不会,然后看一下RoboSpice项目,它有与DownloadManager类似的理念。
答案 1 :(得分:0)
回答这个问题的一个子部分。你之所以得到“占位符”字符串的原因是因为reason列是一个整数,而不是一个String。请参阅Android DownloadManager: Download fails, but COLUMN_REASON only returns “placeholder”。