【问题标题】:Download failed with error 400 when using Android Pie使用 Android Pie 时下载失败并出现错误 400
【发布时间】:2019-05-27 21:06:58
【问题描述】:

我一直在生产应用程序中使用 DownloadManager 没有问题,但现在它在 Android Pie (API 28) 中失败了。请注意,下载被标记为失败并返回错误 400。

@Override
public void onReceive(Context context, Intent intent) {
    long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(reference);
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor cursor = downloadManager.query(query);
    cursor.moveToFirst();
    int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
    int status = cursor.getInt(statusIndex);

    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        int fileUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String savedFileUri = cursor.getString(fileUriIndex);

        ParcelFileDescriptor pfd = null;
        try {
            pfd = context.getContentResolver().openFileDescriptor(Uri.parse(savedFileUri), "r");
            PamplonaParkingXMLParser pamplonaParkingXMLParser = new PamplonaParkingXMLParser();
            ParkingList parkingList = pamplonaParkingXMLParser.parse(pfd);
            ObservableObject.getInstance().updateValue(parkingList);
            if (pfd != null) {
                pfd.close();
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, e.getDetail().getLocalizedMessage());
        } catch (IOException | NullPointerException e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
    } else if(status == DownloadManager.STATUS_FAILED){
        Log.e(TAG, "Download failed");
        int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(reasonIndex);
        Log.e(TAG, reason +"");
    }
}

【问题讨论】:

    标签: android android-download-manager android-9.0-pie


    【解决方案1】:

    试试这些解决方案

    解决方案 1)

    manifest文件的应用标签中添加这一行

    android:usesCleartextTraffic="true"
    

    如下图

    <application
                android:name=".ApplicationClass"
                android:usesCleartextTraffic="true"
                android:networkSecurityConfig="@xml/network_security_config"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
    

    manifest文件的应用标签中添加这个标签

    解决方案 2)

    在应用标签中添加android:networkSecurityConfig="@xml/network_security_config"

    <application
            android:name=".ApplicationClass"
            android:networkSecurityConfig="@xml/network_security_config"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    

    在哪里network_security_config.xml

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true" />
    </network-security-config>
    

    在res目录下创建xml,然后在xml文件夹下创建network_security_config.xml 有关更多信息,请参阅我的回答 Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

    【讨论】:

    • 谢谢,我正在使用 等试图明确地只允许到域的明文流量,但没有奏效。您的解决方案终于解决了问题。
    • 我很高兴它成功了,请接受我对其他人的回答,以便其他人可以使用它
    • 默认情况下为所有域设置 cleartextTraffic 权限不是很安全的解决方法。我徘徊,如果有可能发现究竟是什么导致错误 400。就我而言,我知道特定的 URL 和域,但是 根本不起作用:(
    • 尝试解决方案 2 并告诉我
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多