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开发 开发者
Android Studio详细下载,安装使用教程
Android Studio详细下载,安装使用教程
297 0
|
4月前
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
132 0
|
1月前
|
Shell 开发工具 Android开发
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
459 2
|
1月前
|
Android开发 对象存储
OSS对象储存android开发进行下载到本地文件时异步操作失效
android vivo80使用官方示例代码进行文件下载,但是使用oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>()时onSuccess和onFailure不执行
|
4月前
|
XML Java Android开发
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
94 0
|
4月前
|
缓存 IDE Java
应用研发平台EMAS中classpath 'com.aliyun.ams:alicloud-android-networkmonitor-plugin:1.3.0-open'这个一直下载不成功,这个需要怎么处理?
应用研发平台EMAS中classpath 'com.aliyun.ams:alicloud-android-networkmonitor-plugin:1.3.0-open'这个一直下载不成功,这个需要怎么处理?
51 1
|
8月前
|
Linux 开发工具 Android开发
Android Studio 安装教程 下载最新版 Windows(详细步骤)2021.2.1版本最新版
Android Studio 安装教程 下载最新版 Windows(详细步骤)2021.2.1版本最新版
|
8月前
|
Android开发
Android中 Download Manager系统下载管理器在Android 10系统中无法使用的情况
Android中 Download Manager系统下载管理器在Android 10系统中无法使用的情况
234 0
|
10月前
|
XML Shell Linux
Windows下成功安装Repo和下载Android源码方法总结
Windows下成功安装Repo和下载Android源码方法总结