From 888ce12ddc13601fd63ecd3d27d6e74365c800d1 Mon Sep 17 00:00:00 2001 From: jenly1314 Date: Tue, 31 Dec 2019 17:33:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=20(#7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../king/app/updater/http/HttpManager.java | 101 +++++++++++------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java index 10eb5c2..7714af9 100644 --- a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java +++ b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java @@ -24,6 +24,9 @@ import javax.net.ssl.HttpsURLConnection; */ public class HttpManager implements IHttpManager { + private static final int HTTP_TEMP_REDIRECT = 307; + private static final int HTTP_PERM_REDIRECT = 308; + private static final int DEFAULT_TIME_OUT = 20000; private int mTimeout; @@ -91,12 +94,7 @@ public class HttpManager implements IHttpManager { } - @Override - protected File doInBackground(Void... voids) { - - try { - HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory()); - HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier()); + private File download(String url) throws Exception{ HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection(); connect.setRequestMethod("GET"); connect.setRequestProperty("Accept-Encoding", "identity"); @@ -111,51 +109,73 @@ public class HttpManager implements IHttpManager { } connect.connect(); - int responseCode = connect.getResponseCode(); - Log.d(Constants.TAG,"Content-Type:" + connect.getContentType()); - if(responseCode == HttpURLConnection.HTTP_OK){ - InputStream is = connect.getInputStream(); + Log.d(Constants.TAG,"Content-Type:" + connect.getContentType()); + int responseCode = connect.getResponseCode(); + switch (responseCode){ + case HttpURLConnection.HTTP_OK: { + InputStream is = connect.getInputStream(); - long length = connect.getContentLength(); + long length = connect.getContentLength(); - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - length = connect.getContentLengthLong(); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + length = connect.getContentLengthLong(); + } - Log.d(Constants.TAG,"contentLength:" + length); + Log.d(Constants.TAG, "contentLength:" + length); + + long progress = 0; + + byte[] buffer = new byte[8192]; + + int len; + File file = new File(path, filename); + FileOutputStream fos = new FileOutputStream(file); + while ((len = is.read(buffer)) != -1) { + if (isCancel) { + cancel(true); + break; + } + fos.write(buffer, 0, len); + progress += len; + //更新进度 + if (length > 0) { + publishProgress(progress, length); + } + } - long progress = 0; + fos.flush(); + fos.close(); + is.close(); - byte[] buffer = new byte[8192]; + connect.disconnect(); - int len; - File file = new File(path,filename); - FileOutputStream fos = new FileOutputStream(file); - while ((len = is.read(buffer)) != -1){ - if(isCancel){ - cancel(true); - break; - } - fos.write(buffer,0,len); - progress += len; - //更新进度 - if(length>0){ - publishProgress(progress,length); - } + return file; } + case HttpURLConnection.HTTP_MULT_CHOICE: + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + case HttpURLConnection.HTTP_SEE_OTHER: + case HTTP_TEMP_REDIRECT: + case HTTP_PERM_REDIRECT: {//重定向 + String redirectUrl = connect.getHeaderField("Location"); + Log.d(Constants.TAG,"redirectUrl = " + redirectUrl); + connect.disconnect(); + return download(redirectUrl); + } + default://连接失败 + throw new ConnectException(String.format("responseCode = %d",responseCode)); - fos.flush(); - fos.close(); - is.close(); - - connect.disconnect(); - - return file; - }else {//连接失败 - throw new ConnectException(String.format("responseCode = %d",responseCode)); } + } + @Override + protected File doInBackground(Void... voids) { + + try{ + HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory()); + HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier()); + return download(url); } catch (Exception e) { this.exception = e; e.printStackTrace(); @@ -196,7 +216,6 @@ public class HttpManager implements IHttpManager { } } - @Override protected void onCancelled() { super.onCancelled();