我已经定义了一个自定义的网络安全配置,在我的清单包括它作为推荐这里
RES/XML/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">127.0.0.1</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
这是我的Android.manifest:
<application android:icon="@drawable/icon"
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:persistent="true" >
Run Code Online (Sandbox Code Playgroud)
尝试通过HTTP与127.0.0.1进行通信时,即使有这些更改,我也会在logcat中看到这一点:
08-09 10:50:34.395 30791 3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
08-09 10:50:34.397 30791 3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
08-09 10:50:34.401 30791 3607 W DownloadManager: [647] Stop requested with status HTTP_DATA_ERROR: Cleartext HTTP traffic to 127.0.0.1 not permitted
08-09 10:50:34.402 30791 …Run Code Online (Sandbox Code Playgroud) 我正在尝试开发显示视频的应用程序,您可以下载它我正在使用下载管理器类,但它没有用,也没有给我任何错误:(
这是我的下载管理器代码:
public void downloadFileFromUrl(String url, String fileName) {
String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";
File folder = new File(filePath);
if (!folder.exists()) {
folder.mkdirs();
}
try {
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
long id= downloadManager.enqueue(request);
Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我的称呼
downloadFileFromUrl(path, fileName);
Run Code Online (Sandbox Code Playgroud)
在哪里:
路径:“192.168.1.5:8080/BlueNet_NMC/blue_elephant.mp4”
文件名:“blue_elephant.mp4”
我已经将此权限授予清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" …Run Code Online (Sandbox Code Playgroud)