java - 为什么在 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;
}
解决方案
因为你的远程服务不允许 HTTP 协议,只有 https。
推荐阅读
- google-sheets - 对其他范围内所有出现的范围值求和
- android - 数组需要什么格式才能让微调器正确使用它?
- swift - 当 swift iOS 中出现意外值时,如何将 nil 设置为可编码枚举
- python - 通过 pip 安装模块后在 python 中“找不到模块”
- apache - Issue with apache proxy configuration for Web socket
- apache - Apache vhost - DirectoryIndex 不起作用
- jenkins - Jenkins - 将本地代码文件复制到 Jenkins 工作区所需的帮助
- react-native - setScreenName 上的 react-native 0.64.2 firebase 编译错误
- javascript - 如何通过事件委托收听不同的侦听器
- sharepoint - 当用户在 web 应用程序中进行搜索时,此页面不会与您共享