首页 > 解决方案 > 为什么在 Android 中下载 https 时,http URL 不以编程方式下载 APK?

问题描述

我想问一个关于在 Android 中以编程方式下载 Apk 的问题。当我通过 http 提供 url 时,Apk 没有下载到我的手机上。但是使用 https 更改 URL 可以成功下载 APK。这是什么原因?此外,下载网址隐藏在 https 网站上。

        try {

            URL url = new URL("http://....../LinaSoft_1_0_4.Apk");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.connect();


            String PATH = Environment.getExternalStorageDirectory()+"/Download/";
            File file = new File(PATH);
            file.mkdirs();

            File outputFile = new File(file,"LinaSoft_1_0_4.apk");
            Log.d("output",outputFile.toString());

            File dosyalar = new File(PATH);


            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();
            int total_size = c.getContentLength();


            byte[] buffer = new byte[1024];
            int len1 = 0;
            int per = 0;
            int downloaded=0;

            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                downloaded +=len1;
                per = (int) (downloaded * 100 / total_size);
                publishProgress(per);
            }
            fos.close();
            is.close();
            Log.d("Boyut",String.valueOf(total_size));
            OpenNewVersion(PATH);

            flag = true;
        } catch (IOException e) {
            Log.d( "Update Error: " , e.getMessage());
            flag = false;
        }
        return flag;

    }

标签: javaandroid

解决方案


因为你的远程服务不允许 HTTP 协议,只有 https。


推荐阅读