File download fails for version below API21

0

My app downloads simple text files from my website. For API21 and above, all works fine, but for API18 & API 19, the download fails. The app uses download manager, but when that failed, I wrote this alternative code to try to figure out the problem:

public int DownloadFiles(){
    int gotFileOK=1;//indicates download OK
    File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "TestAMJ");
    if (folder.exists()) {
        Log.v("sTag", "the folder |"+folder+"| exists");
    } else {
        Log.v("sTag","the folder |"+folder+"| does not exist");
        File dirSetFile = new File(Environment.getExternalStorageDirectory() + File.separator + "TestAMJ");
        dirSetFile.mkdirs();
    }
    File tempFil = new File(Environment.getExternalStorageDirectory() + File.separator + "TestAMJ" + File.separator + "test.txt");
    try {
        URL u = new URL("https://aboutmyjourney.com/wp-content/uploads/developerlist.txt");
            InputStream is = u.openStream();
            DataInputStream dis = new DataInputStream(is);
        byte[] buffer = new byte[1024];
        int length;
        FileOutputStream fos = new FileOutputStream(tempFil);
        while ((length = dis.read(buffer))>0) {
            fos.write(buffer, 0, length);
        }
    } catch (IOException ioe) {
        gotFileOK=2;
        Log.v("sTag", "io error", ioe);
    }
    return(gotFileOK);
}

As before, this worked fine wit API21 or above. However, with an API19 emulator, I got this error message:

04-23 15:48:46.798 5167-5183/com.example.downloadalternative V/sTag: io error java.net.ConnectException: failed to connect to aboutmyjourney.com/2001:8d8:100f:f000::2fa (port 443): connect failed: ETIMEDOUT (Connection timed out) at libcore.io.IoBridge.connect(IoBridge.java:114) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) at java.net.Socket.connect(Socket.java:843) at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131) at com.android.okhttp.Connection.connect(Connection.java:101) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294) at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246) at java.net.URL.openStream(URL.java:470) at com.example.downloadalternative.DownloadActivity.DownloadFiles(DownloadActivity.java:69) at com.example.downloadalternative.DownloadActivity$1.run(DownloadActivity.java:43) at java.lang.Thread.run(Thread.java:841) Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out) at libcore.io.Posix.connect(Native Method) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) at libcore.io.IoBridge.connectErrno(IoBridge.java:127) at libcore.io.IoBridge.connect(IoBridge.java:112) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) at java.net.Socket.connect(Socket.java:843) at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131) at com.android.okhttp.Connection.connect(Connection.java:101) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294) at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246) at java.net.URL.openStream(URL.java:470) at com.example.downloadalternative.DownloadActivity.DownloadFiles(DownloadActivity.java:69) at com.example.downloadalternative.DownloadActivity$1.run(DownloadActivity.java:43) at java.lang.Thread.run(Thread.java:841) 04-23 15:48:46.798 5167-5183/com.example.downloadalternative V/sTag: Result is 2

and with API18 I got:

04-23 16:02:33.322 1982-2019/com.example.downloadalternative V/sTag: io error javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85c0110: Failure in SSL library, usually a protocol error error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x918257f1:0x00000000) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:441) at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:231) at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442) at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282) at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) at java.net.URL.openStream(URL.java:462) at com.example.downloadalternative.DownloadActivity.DownloadFiles(DownloadActivity.java:69) at com.example.downloadalternative.DownloadActivity$1.run(DownloadActivity.java:43) at java.lang.Thread.run(Thread.java:841) Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb85c0110: Failure in SSL library, usually a protocol error error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x918257f1:0x00000000) at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:399) ... 12 more 04-23 16:02:33.322 1982-2019/com.example.downloadalternative V/sTag: Result is 2

I guess that this is related to support for versions of SSL/TLS, but I am a complete novice when it comes to internet security. I found this question: how-to-fix-expected-android-api-level-21-but-was-19-in-android but none of the suggestions worked (maybe because I didn't understand them properly!)

Can anyone tell me how to make HTTPS download work for API18/19 (and still have it work for all the more recent API levels as well)?

https
download
asked on Stack Overflow Apr 23, 2021 by Chris S

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0