android自更新时下载出现的问题

简介:

之前做过一个电视台app,电视台app每次启动时会访问服务器,判断是否需要下载新版本

但是下载时老是下载失败,apk包总是下载不下来.到底是什么原因呢?

服务器下载接口如下:

Java代码   收藏代码
  1. /*** 
  2.      * 下载apk 
  3.      * @param path 
  4.      * @param request 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     @RequestMapping(value = "/download"/*, headers = {"content-type=application/json"}*/)  
  9.     public ResponseEntity<byte[]> download( String path,HttpServletRequest request) throws IOException {  
  10.         AccessLog accessLog=logInto(request);  
  11.         accessLog.setDescription("下载客户端");  
  12.         if(!ValueWidget.isNullOrEmpty(request.getContentType())&& request.getContentType().toLowerCase().contains("application/json")){  
  13.             String requestStr=WebServletUtil.getRequestQueryStr(request, null);  
  14.             System.out.println(requestStr);  
  15.             Map queryMap=JSONPUtil.getMapFromJson(requestStr);  
  16.             if(!ValueWidget.isNullOrEmpty(queryMap)){  
  17.                 path=(String) queryMap.get("path");  
  18.             }  
  19.         }  
  20.         if(ValueWidget.isNullOrEmpty(path)){  
  21.             System.out.println("download failed");  
  22.             accessLog.setOperateResult("下载失败,没有传递path参数");  
  23.             logSave(accessLog, request);  
  24.             return null;  
  25.         }  
  26.         String realpath =WebServletUtil.getUploadPath(request, "upload/download/apk", request  
  27.                 .getSession().getServletContext(), Constant2.SRC_MAIN_WEBAPP);  
  28.         if(!realpath.endsWith(File.separator)){  
  29.             realpath=realpath+File.separator;  
  30.         }  
  31.         HttpHeaders headers = new HttpHeaders();  
  32.         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
  33.         String fullpath=realpath+path;  
  34.           
  35.         System.out.println("download path:"+fullpath);  
  36.         headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, path));  
  37.         accessLog.setOperateResult("下载成功,下载文件:"+fullpath+" ,size:"+FileUtils.getFileSize2(fullpath));  
  38.         logSave(accessLog, request);  
  39.         return new ResponseEntity<byte[]>(FileUtils.getBytes4File(fullpath),  
  40.                                           headers, HttpStatus.CREATED);  
  41.     }  

 

安卓端调用的下载方法(核心代码)如下:

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param huc 
  4.      * @param sendBytes 
  5.      * @param mode 
  6.      * @param isWrite2file 
  7.      *            : 是否写入文件 
  8.      * @return 
  9.      * @throws Exception 
  10.      */  
  11.     private static byte[] connection(HttpURLConnection huc,  
  12.             boolean isWrite2file, Object file, String sizeHeadKey)  
  13.             throws Exception {  
  14.         int resCode = huc.getResponseCode();  
  15.   
  16.         if (resCode == HttpURLConnection.HTTP_OK) {  
  17.             int contentLength = 0;  
  18.             if (ValueWidget.isNullOrEmpty(sizeHeadKey)) {// 若header中没有size  
  19.                 contentLength = huc.getContentLength();  
  20.             } else {  
  21.                 String sizeHeaderValue = huc.getHeaderField(sizeHeadKey);  
  22.                 if (!ValueWidget.isNullOrEmpty(sizeHeaderValue)) {  
  23.                     contentLength = Integer.parseInt(sizeHeaderValue);  
  24.                 }  
  25.   
  26.             }  
  27.             if (isDetail) {  
  28.                 System.out  
  29.                         .println("[connection]contentLength:" + contentLength);  
  30.                 responseHeaderFields = huc.getHeaderFields();  
  31.                 String downloadHeader = "Content-Disposition";  
  32.                 if (!ValueWidget.isNullOrEmpty(responseHeaderFields)) {  
  33.                     List<String> ContentDispositions = responseHeaderFields  
  34.                             .get(downloadHeader);  
  35.                     if (!ValueWidget.isNullOrEmpty(ContentDispositions)) {  
  36.                         String ContentDisposition = ContentDispositions.get(0);  
  37.                         System.out.println("ContentDisposition:"  
  38.                                 + ContentDisposition);  
  39.                         System.out.println("ContentDisposition convertISO2UTF:"  
  40.                                 + SystemHWUtil  
  41.                                         .convertISO2UTF(ContentDisposition));  
  42.                         System.out  
  43.                                 .println("ContentDisposition convertISO2GBK: "  
  44.                                         + SystemHWUtil  
  45.                                                 .convertISO2GBK(ContentDisposition));  
  46.                     }  
  47.                 }  
  48.                 for (Object obj : responseHeaderFields.keySet()) {  
  49.                     List<String> list = responseHeaderFields.get(obj);  
  50.                     if (!ValueWidget.isNullOrEmpty(list)) {  
  51.                         System.out.println(obj + " : "  
  52.                                 + SystemHWUtil.formatArr(list, ";"));  
  53.                     }  
  54.                 }  
  55.                 System.out  
  56.                         .println("[connection]contentLength:" + contentLength);  
  57.             }  
  58.             if (contentLength > 0) {  
  59.                 if (isDetail)  
  60.                     System.out  
  61.                             .println("[HttpSocketUtil.connection]httputil,contentLength:"  
  62.                                     + contentLength);  
  63.                 // return readData(huc);  
  64.                 File file2 = null;  
  65.                 if (isWrite2file) {  
  66.                     if (file instanceof File) {  
  67.                         file2 = (File) file;  
  68.                         writeFileFromLength(huc, contentLength, file2);  
  69.                         if (isDetail) {  
  70.                             System.out.println("download success:"  
  71.                                     + file2.getAbsolutePath());  
  72.                         }  
  73.                     } else {  
  74.                         writeFileFromLength(huc, contentLength,  
  75.                                 (OutputStream) file);  
  76.                     }  
  77.                     return null;  
  78.                 } else {  
  79.                     return readDataFromLength(huc, contentLength);  
  80.                 }  
  81.             } else {  
  82.                 if (isWrite2file) {  
  83.                     InputStream in = huc.getInputStream();  
  84.                     FileUtils.writeIn2OutputCloseAll(in, new FileOutputStream(  
  85.                             (File) file));  
  86.                     if (isDetail) {  
  87.                         System.out.println("download success:"  
  88.                                 + ((File) file).getAbsolutePath());  
  89.                     }  
  90.                     return null;  
  91.                 }  
  92.                 return readData(huc);  
  93.             }  
  94.         } else {  
  95.             System.out.println("response Code:" + resCode);  
  96.         }  
  97.         return null;  
  98.     }  

 

代码本身是没有逻辑错误的.花了很长时间才找到原因,是定义的response的status code不一致.



 

 

后台设置的status code是201,而Android端判断的status code是200,不一致导致下载流程没有走到下载逻辑.

修改方法:把后台的status code改为200 就OK了

相关文章
|
4月前
|
存储 缓存 Android开发
安卓Jetpack Compose+Kotlin, 使用ExoPlayer播放多个【远程url】音频,搭配Okhttp库进行下载和缓存,播放完随机播放下一首
这是一个Kotlin项目,使用Jetpack Compose和ExoPlayer框架开发Android应用,功能是播放远程URL音频列表。应用会检查本地缓存,如果文件存在且大小与远程文件一致则使用缓存,否则下载文件并播放。播放完成后或遇到异常,会随机播放下一首音频,并在播放前随机设置播放速度(0.9到1.2倍速)。代码包括ViewModel,负责音频管理和播放逻辑,以及UI层,包含播放和停止按钮。
|
5月前
|
开发工具 Android开发 git
Windows下载android2.2完整源码(转)
Windows下载android2.2完整源码(转)
72 3
|
5月前
|
敏捷开发 Java 机器人
云效产品使用常见问题之打包后的Android应用获取下载地址失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
5月前
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
655 0
|
2月前
|
Ubuntu 开发工具 Android开发
Repo下载AOSP源码:基于ubuntu22.04 环境配置,android-12.0.0_r32
本文介绍了在基于Ubuntu 22.04的环境下配置Python 3.9、安装repo工具、下载和同步AOSP源码包以及处理repo同步错误的详细步骤。
118 0
Repo下载AOSP源码:基于ubuntu22.04 环境配置,android-12.0.0_r32
|
5月前
|
Java Android开发
android 下载图片的问题
android 下载图片的问题
39 3
|
2月前
|
开发工具 uml git
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
本文分享了下载AOSP源码的方法,包括如何使用repo工具和处理常见的repo sync错误,以及配置Python环境以确保顺利同步特定版本的AOSP代码。
169 0
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
|
2月前
|
API 开发工具 Android开发
Android源码下载
Android源码下载
156 0
|
2月前
|
存储 监控 数据库
Android经典实战之OkDownload的文件分段下载及合成原理
本文介绍了 OkDownload,一个高效的 Android 下载引擎,支持多线程下载、断点续传等功能。文章详细描述了文件分段下载及合成原理,包括任务创建、断点续传、并行下载等步骤,并展示了如何通过多种机制保证下载的稳定性和完整性。
35 0
|
2月前
|
Ubuntu 开发工具 Android开发
Repo下载、编译AOSP源码:基于Ubuntu 21.04,android-12.1.0_r27
文章记录了作者在Ubuntu 21.04服务器上配置环境、下载并编译基于Android 12.1.0_r27版本的AOSP源码的过程,包括解决编译过程中遇到的问题和错误处理方法。
56 0
下一篇
无影云桌面