简体   繁体   English

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

[英]download a text-file from Webserver using Asynctask

/**
 * 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 is running until "conection.connect();" doInBackground 一直运行到“connection.connect();” After about 3 minutes the app will continue with an exception: "Failed to connect to /xxx.168.2.200:80" In principle, I can access the web server's HTML pages with my mobile phone.大约3分钟后app会继续出现异常:“Failed to connect to /xxx.168.2.200:80” 原则上我可以用手机访问web服务器的HTML页面。 My question: What does the error message mean and how can I fix the error?我的问题:错误消息是什么意思,我该如何解决这个错误?

networkSecurityConfig: not added to manifest, how to formulate? networkSecurityConfig:未添加到清单中,如何制定?

app targets android 9, API 28 only partially installed应用程序目标 android 9,API 28 仅部分安装

I will use the app only for local adresses我只会将应用程序用于本地地址

Follows manifest: enter code here遵循清单: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM