使用Android Pie时,下载失败,错误400

问题描述 投票:2回答:1

我一直在生产应用程序中使用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个回答
6
投票

试试这些解决方案

解决方案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然后在network_security_config.xml文件夹中创建xml有关更多信息,请参阅我的答案Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

© www.soinside.com 2019 - 2024. All rights reserved.