From 85f3616e46f63f6c08dde2c3485d176c3aa0a222 Mon Sep 17 00:00:00 2001 From: HYL123698745 <1252347619@qq.com> Date: Wed, 7 Jul 2021 10:48:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E3=80=91=EF=BC=9A=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/config.json | 70 ++++- .../quickstart/xutils/http/BaseParams.java | 3 +- .../xutils/http/body/MultipartBody.java | 7 +- .../xutils/http/request/HttpRequest.java | 264 +++++++++--------- .../com/wordplat/quickstart/xutils/x.java | 9 - 5 files changed, 205 insertions(+), 148 deletions(-) diff --git a/entry/src/main/config.json b/entry/src/main/config.json index 5d5031f..26946a5 100644 --- a/entry/src/main/config.json +++ b/entry/src/main/config.json @@ -16,16 +16,72 @@ ], "reqPermissions": [ { - "name": "ohos.permission.INTERNET" + "name": "ohos.permission.INTERNET", + "reason": "get internet info", + "usedScene": { + "ability": [ + "com.wordplat.quickstart.ability.MainAbility", + "com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability", + "com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability", + "com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability", + "com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability", + "com.wordplat.quickstart.ability.With_RecyclerView_Example_Ability" + ], + "when": "always" + } }, { - "name": "ohos.permission.WRITE_MEDIA" + "name": "ohos.permission.WRITE_MEDIA", + "reason": "get write info", + "usedScene": { + "ability": [ + "com.wordplat.quickstart.ability.MainAbility", + "com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability", + "com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability", + "com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability", + "com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability", + "com.wordplat.quickstart.ability.With_RecyclerView_Example_Ability" + ], + "when": "always" + } }, { - "name": "ohos.permission.GET_WIFI_INFO" + "name": "ohos.permission.GET_WIFI_INFO", + "reason": "get wifi info", + "usedScene": { + "ability": [ + "com.wordplat.quickstart.ability.MainAbility", + "com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability", + "com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability", + "com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability", + "com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability", + "com.wordplat.quickstart.ability.With_RecyclerView_Example_Ability" + ], + "when": "always" + } }, { - "name": "ohos.permission.GET_NETWORK_INFO" + "name": "ohos.permission.GET_NETWORK_INFO", + "reason": "get network info", + "usedScene": { + "ability": [ + "com.wordplat.quickstart.ability.MainAbility", + "com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability", + "com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability", + "com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability", + "com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability", + "com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability", + "com.wordplat.quickstart.ability.With_RecyclerView_Example_Ability" + ], + "when": "always" + } } ], "distro": { @@ -73,6 +129,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.Enable_Left_And_Right_Refresh_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" @@ -89,6 +146,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.Disable_Left_And_Right_Refresh_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" @@ -105,6 +163,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.MACD_RSI_KDJ_Show_Together_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" @@ -120,6 +179,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.Simple_TimeLine_Example_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" @@ -135,6 +195,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.With_Pull_To_Refresh_Example_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" @@ -150,6 +211,7 @@ "orientation": "unspecified", "name": "com.wordplat.quickstart.ability.With_Fragment_And_TabLayout_Switcher_Example_Ability", "icon": "$media:icon", + "visible": false, "label": "$string:app_name", "type": "page", "launchType": "standard" diff --git a/entry/src/main/java/com/wordplat/quickstart/xutils/http/BaseParams.java b/entry/src/main/java/com/wordplat/quickstart/xutils/http/BaseParams.java index 919c98d..06f8930 100644 --- a/entry/src/main/java/com/wordplat/quickstart/xutils/http/BaseParams.java +++ b/entry/src/main/java/com/wordplat/quickstart/xutils/http/BaseParams.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Array; +import java.security.NoSuchAlgorithmException; import java.util.*; /** @@ -339,7 +340,7 @@ public abstract class BaseParams { * @return RequestBody * @throws IOException */ - public RequestBody getRequestBody() throws IOException { + public RequestBody getRequestBody() throws IOException, NoSuchAlgorithmException { checkBodyParams(); if (this.requestBody != null) { return this.requestBody; diff --git a/entry/src/main/java/com/wordplat/quickstart/xutils/http/body/MultipartBody.java b/entry/src/main/java/com/wordplat/quickstart/xutils/http/body/MultipartBody.java index f5fe776..db9e87d 100644 --- a/entry/src/main/java/com/wordplat/quickstart/xutils/http/body/MultipartBody.java +++ b/entry/src/main/java/com/wordplat/quickstart/xutils/http/body/MultipartBody.java @@ -9,6 +9,8 @@ import com.wordplat.quickstart.xutils.http.BaseParams.BodyItemWrapper; import com.wordplat.quickstart.xutils.http.ProgressHandler; import java.io.*; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.util.List; import java.util.Random; import java.util.concurrent.atomic.AtomicLong; @@ -30,7 +32,8 @@ public class MultipartBody implements ProgressBody { private final List multipartParams; private long total = 0; private long current = 0; - private final Random random = new Random(); + + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); /** * \ * MultipartBody @@ -38,7 +41,7 @@ public class MultipartBody implements ProgressBody { * @param multipartParams * @param charset */ - public MultipartBody(List multipartParams, String charset) { + public MultipartBody(List multipartParams, String charset) throws NoSuchAlgorithmException { if (!TextUtils.isEmpty(charset)) { this.charset = charset; } diff --git a/entry/src/main/java/com/wordplat/quickstart/xutils/http/request/HttpRequest.java b/entry/src/main/java/com/wordplat/quickstart/xutils/http/request/HttpRequest.java index 07c280a..bbd70d5 100644 --- a/entry/src/main/java/com/wordplat/quickstart/xutils/http/request/HttpRequest.java +++ b/entry/src/main/java/com/wordplat/quickstart/xutils/http/request/HttpRequest.java @@ -98,138 +98,138 @@ public class HttpRequest extends UriRequest { @Override public void sendRequest() throws Throwable { - isLoading = false; - responseCode = 0; - - URL url = new URL(queryUrl); - - Proxy proxy = params.getProxy(); - if (proxy != null) { - connection = (HttpURLConnection) url.openConnection(proxy); - } else { - connection = (HttpURLConnection) url.openConnection(); - } - connection.setReadTimeout(params.getReadTimeout()); - connection.setConnectTimeout(params.getConnectTimeout()); - connection.setInstanceFollowRedirects(params.getRedirectHandler() == null); - if (connection instanceof HttpsURLConnection) { - SSLSocketFactory sslSocketFactory = params.getSslSocketFactory(); - if (sslSocketFactory != null) { - ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory); - } - - HostnameVerifier hostnameVerifier = params.getHostnameVerifier(); - if (hostnameVerifier != null) { - ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier); - } - } - - if (params.isUseCookie()) { - try { - Map> singleMap = - COOKIE_MANAGER.get(url.toURI(), new HashMap>(0)); - List cookies = singleMap.get("Cookie"); - if (cookies != null) { - connection.setRequestProperty("Cookie", TextUtils.join(";", cookies)); - } - } catch (Throwable ex) { - LogUtil.e(ex.getMessage(), ex); - } - } - - List headers = params.getHeaders(); - if (headers != null) { - for (RequestParams.Header header : headers) { - String name = header.key; - String value = header.getValueStrOrNull(); - if (!TextUtils.isEmpty(name)) { - if (header.setHeader) { - connection.setRequestProperty(name, value); - } else { - connection.addRequestProperty(name, value); - } - } - } - } - if (responseParser != null) { - responseParser.beforeRequest(this); - } - if (requestInterceptListener != null) { - requestInterceptListener.beforeRequest(this); - } - - { // write body - HttpMethod method = params.getMethod(); - try { - connection.setRequestMethod(method.toString()); - } catch (ProtocolException ex) { - try { // fix: HttpURLConnection not support PATCH method. - Field methodField = HttpURLConnection.class.getDeclaredField("method"); - methodField.setAccessible(true); - methodField.set(connection, method.toString()); - } catch (Throwable ignored) { - throw ex; - } - } - if (HttpMethod.permitsRequestBody(method)) { - RequestBody body = params.getRequestBody(); - if (body != null) { - if (body instanceof ProgressBody) { - ((ProgressBody) body).setProgressHandler(progressHandler); - } - String contentType = body.getContentType(); - if (!TextUtils.isEmpty(contentType)) { - connection.setRequestProperty("Content-Type", contentType); - } - boolean isChunkedMode = false; - long contentLength = body.getContentLength(); - if (contentLength < 0) { - connection.setChunkedStreamingMode(256 * 1024); - isChunkedMode = true; - } else { - if (contentLength < Integer.MAX_VALUE) { - connection.setFixedLengthStreamingMode((int) contentLength); - } else { - connection.setChunkedStreamingMode(256 * 1024); - isChunkedMode = true; - } - } - - if (isChunkedMode) { - connection.setRequestProperty("Transfer-Encoding", "chunked"); - } else { - connection.setRequestProperty("Content-Length", String.valueOf(contentLength)); - } - - connection.setDoOutput(true); - body.writeTo(connection.getOutputStream()); - } - } - } - - responseCode = connection.getResponseCode(); - { - if (responseParser != null) { - responseParser.afterRequest(this); - } - if (requestInterceptListener != null) { - requestInterceptListener.afterRequest(this); - } - } - if (responseCode == 204 || responseCode == 205) { // empty content - throw new HttpException(responseCode, this.getResponseMessage()); - } else if (responseCode >= 300) { - HttpException httpException = new HttpException(responseCode, this.getResponseMessage()); - try { - httpException.setResult(IOUtil.readStr(this.getInputStream(), params.getCharset())); - } catch (Throwable ex) { - LogUtil.w(ex.getMessage(), ex); - } - LogUtil.e(httpException.toString() + ", url: " + queryUrl); - throw httpException; - } - - isLoading = true; +// isLoading = false; +// responseCode = 0; +// +// URL url = new URL(queryUrl); +// +// Proxy proxy = params.getProxy(); +// if (proxy != null) { +// connection = (HttpURLConnection) url.openConnection(proxy); +// } else { +// connection = (HttpURLConnection) url.openConnection(); +// } +// connection.setReadTimeout(params.getReadTimeout()); +// connection.setConnectTimeout(params.getConnectTimeout()); +// connection.setInstanceFollowRedirects(params.getRedirectHandler() == null); +// if (connection instanceof HttpsURLConnection) { +// SSLSocketFactory sslSocketFactory = params.getSslSocketFactory(); +// if (sslSocketFactory != null) { +// ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory); +// } +// +// HostnameVerifier hostnameVerifier = params.getHostnameVerifier(); +// if (hostnameVerifier != null) { +// ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier); +// } +// } +// +// if (params.isUseCookie()) { +// try { +// Map> singleMap = +// COOKIE_MANAGER.get(url.toURI(), new HashMap>(0)); +// List cookies = singleMap.get("Cookie"); +// if (cookies != null) { +// connection.setRequestProperty("Cookie", TextUtils.join(";", cookies)); +// } +// } catch (Throwable ex) { +// LogUtil.e(ex.getMessage(), ex); +// } +// } +// +// List headers = params.getHeaders(); +// if (headers != null) { +// for (RequestParams.Header header : headers) { +// String name = header.key; +// String value = header.getValueStrOrNull(); +// if (!TextUtils.isEmpty(name)) { +// if (header.setHeader) { +// connection.setRequestProperty(name, value); +// } else { +// connection.addRequestProperty(name, value); +// } +// } +// } +// } +// if (responseParser != null) { +// responseParser.beforeRequest(this); +// } +// if (requestInterceptListener != null) { +// requestInterceptListener.beforeRequest(this); +// } +// +// { // write body +// HttpMethod method = params.getMethod(); +// try { +// connection.setRequestMethod(method.toString()); +// } catch (ProtocolException ex) { +// try { // fix: HttpURLConnection not support PATCH method. +// Field methodField = HttpURLConnection.class.getDeclaredField("method"); +// methodField.setAccessible(true); +// methodField.set(connection, method.toString()); +// } catch (Throwable ignored) { +// throw ex; +// } +// } +// if (HttpMethod.permitsRequestBody(method)) { +// RequestBody body = params.getRequestBody(); +// if (body != null) { +// if (body instanceof ProgressBody) { +// ((ProgressBody) body).setProgressHandler(progressHandler); +// } +// String contentType = body.getContentType(); +// if (!TextUtils.isEmpty(contentType)) { +// connection.setRequestProperty("Content-Type", contentType); +// } +// boolean isChunkedMode = false; +// long contentLength = body.getContentLength(); +// if (contentLength < 0) { +// connection.setChunkedStreamingMode(256 * 1024); +// isChunkedMode = true; +// } else { +// if (contentLength < Integer.MAX_VALUE) { +// connection.setFixedLengthStreamingMode((int) contentLength); +// } else { +// connection.setChunkedStreamingMode(256 * 1024); +// isChunkedMode = true; +// } +// } +// +// if (isChunkedMode) { +// connection.setRequestProperty("Transfer-Encoding", "chunked"); +// } else { +// connection.setRequestProperty("Content-Length", String.valueOf(contentLength)); +// } +// +// connection.setDoOutput(true); +// body.writeTo(connection.getOutputStream()); +// } +// } +// } +// +// responseCode = connection.getResponseCode(); +// { +// if (responseParser != null) { +// responseParser.afterRequest(this); +// } +// if (requestInterceptListener != null) { +// requestInterceptListener.afterRequest(this); +// } +// } +// if (responseCode == 204 || responseCode == 205) { // empty content +// throw new HttpException(responseCode, this.getResponseMessage()); +// } else if (responseCode >= 300) { +// HttpException httpException = new HttpException(responseCode, this.getResponseMessage()); +// try { +// httpException.setResult(IOUtil.readStr(this.getInputStream(), params.getCharset())); +// } catch (Throwable ex) { +// LogUtil.w(ex.getMessage(), ex); +// } +// LogUtil.e(httpException.toString() + ", url: " + queryUrl); +// throw httpException; +// } +// +// isLoading = true; } @Override diff --git a/entry/src/main/java/com/wordplat/quickstart/xutils/x.java b/entry/src/main/java/com/wordplat/quickstart/xutils/x.java index dcd54f2..0f261bb 100644 --- a/entry/src/main/java/com/wordplat/quickstart/xutils/x.java +++ b/entry/src/main/java/com/wordplat/quickstart/xutils/x.java @@ -152,14 +152,5 @@ public final class x { public static void setViewInjector(ViewInjector viewInjector) { Ext.viewInjector = viewInjector; } - - /** - * setDefaultHostnameVerifier - * - * @param hostnameVerifier - */ - public static void setDefaultHostnameVerifier(HostnameVerifier hostnameVerifier) { - HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); - } } } -- Gitee