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

4

我在一个生产应用中使用了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 +"");
    }
}
1个回答

10

请尝试以下解决方案

解决方案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>

enter image description here

在res目录下创建xml文件夹,然后在xml文件夹中创建network_security_config.xml。如需更多信息,请参考我的答案。 下载管理器在Android Pie 9.0(小米mi A2)中无法工作

1
谢谢,我一直在使用<domain-config cleartextTrafficPermitted="true">等方式尝试显式允许域名的明文流量,但都没有成功。你的解决方案最终解决了这个问题。 - Javi Vazquez
我很高兴它起作用了,请接受我的答案以便其他人也可以使用它。 - Quick learner
默认情况下为所有域设置明文流量权限并不是解决此问题的安全方式。我想知道是否有可能发现导致400错误的确切原因。在我的情况下,我知道特定的URL和域,但是<domain-config cleartextTrafficPermitted="true">根本不起作用:( - Peter Knut
尝试解决方案2并告诉我。 - Quick learner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接