使用Asynctask从Web服务器下载文本文件


/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString(); // "/storage/emulated/0"
System.out.println("Downloading");
URL url = new URL(f_url[0]); //http://xxx.168.2.200/tmp/bsp1.txt;
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new  FileOutputStream(fileNameDaten);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
String fel=e.getMessage();
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* After completing background task
* **/
@Override
protected void onPostExecute(String file_url) {
System.out.println("Downloaded");
}

}

doInBackground一直运行到";conection.connect(("大约3分钟后,应用程序将继续运行,但出现异常:"未能连接到/xxx.168.2200:80";原则上,我可以用手机访问网络服务器的HTML页面。我的问题是:错误消息是什么意思?我如何修复错误?

networkSecurityConfig:没有添加到清单中,如何制定?

应用程序目标安卓9,API 28仅部分安装

我将使用该应用程序仅用于本地地址

遵循清单:enter code here

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.wicki.pdftextapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UploadToServer"></activity>
</application>
</manifest>

最新更新