2

我正在开发一个应用程序,该应用程序将在通过 oAuth 2 进行身份验证后通过 Rails Heroku 服务器下载存储在 Amazon S3 上的 zip 文件。流程如下:

  1. 请求通过 oAuth2 在 Heroku 上运行的服务器进行身份验证。
  2. 接收 oAuth2 访问令牌。
  3. 请求从服务器下载 zip 文件(将 oAuth 令牌作为承载传递)。
  4. 服务器授权请求并重定向到包含过期签名的 Amazon S3 URL(以阻止任何人在未经身份验证的情况下下载内容)。

此时,我希望 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);

}   
4

2 回答 2

4

I've found in Download Manager sources (line 500):

3xx: redirects (not used by the download manager)

It's not supported, yet.

In my current project, downloads are made in two steps:

  1. Get Amazon url from our own server via oAuth2
  2. Enqueue DownloadManager with the Amazon url.

If you don't like the two step process, I don't, then take a look at RoboSpice project, it has similar philosophy as DownloadManager.

于 2012-11-03T20:03:15.510 回答
1
于 2013-05-09T19:08:28.397 回答