@arnoldo.moen
To download a file in Android over HTTPS, you can use the following steps:
1
|
<uses-permission android:name="android.permission.INTERNET" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.os.AsyncTask; import java.io.InputStream; import java.io.FileOutputStream; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class DownloadFileTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... urls) { try { URL url = new URL(urls[0]); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.connect(); InputStream input = connection.getInputStream(); FileOutputStream output = new FileOutputStream("your_file_name_here"); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } output.close(); input.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return null; } } |
1
|
new DownloadFileTask().execute("https://www.example.com/file_to_download.pdf"); |
Make sure to replace "https://www.example.com/file_to_download.pdf"
with the actual URL of the file you want to download.
By following these steps, you should be able to download a file in Android over HTTPS successfully.