diff --git a/README.md b/README.md index 8c1a44da54cfc7588bd7146ba755950fd35cf74d..1fd04b04b0b86d3212c456628c8d558900289ba7 100755 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - 功能:断线重连、一对一请求、通知、粘性通知、串行请求合并、包分片处理(AbstractFragmentRequestQueue)、缓存、拦截器、支持rxjava,提供类似于retrofit的支持、提供rxjava和rxjava2两种使用方式 - 项目移植状态:主功能已完成 - 调用差异:无 -- 开发版本:sdk5,DevEco Studio 2.1 Release +- 开发版本:sdk6,DevEco Studio2.2 Beta1 - 基线版本:Release v2.0.0 #### 安装教程 @@ -37,7 +37,7 @@ allprojects { ...... } ``` -在sdk5,DevEco Studio 2.1 Release下项目可直接运行 +在sdk6,DevEco Studio2.2 Beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下 @@ -264,7 +264,7 @@ public static void main(String[] args) { #### 测试信息 - CodeCheck代码测试无异常 - CloudTest代码测试无异常 -- 火绒安全病毒安全检测通过 +- 病毒安全检测通过 - 当前版本demo功能与原组件基本无差异 #### 版本迭代 diff --git a/bizsocket_base/build.gradle b/bizsocket_base/build.gradle index cfa5e3143d72bce876b2cf95392afbf3c1fb0794..5534f46ff03e34b3902a6b5e3aa25f53119d8065 100644 --- a/bizsocket_base/build.gradle +++ b/bizsocket_base/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_base/src/main/config.json b/bizsocket_base/src/main/config.json index 1ae43ab9b12e9082348bf752b6e3a14ad7f2d5eb..5fdaf78ff0373173398f83b51a49e1404a338dad 100644 --- a/bizsocket_base/src/main/config.json +++ b/bizsocket_base/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_core/build.gradle b/bizsocket_core/build.gradle index 63e49a7d6236c77ffcd4c529cdb5b4d66a1d10fc..34b9351f3a7cd9d80de9131a9f70c2f7090f4f3e 100644 --- a/bizsocket_core/build.gradle +++ b/bizsocket_core/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_core/src/main/config.json b/bizsocket_core/src/main/config.json index d557abbe5ec7d28edc738421060be4c70065d0a2..5b0db3aa543c9fe3a8bbfd14275585d8c8f4103a 100644 --- a/bizsocket_core/src/main/config.json +++ b/bizsocket_core/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_core/src/main/java/bizsocket/core/SerialSignal.java b/bizsocket_core/src/main/java/bizsocket/core/SerialSignal.java index 0186bd78cfa92e05cf989890120a7af77a6c9120..a02746ba2b325275c5d4c094bfd0ff579f4a2c41 100644 --- a/bizsocket_core/src/main/java/bizsocket/core/SerialSignal.java +++ b/bizsocket_core/src/main/java/bizsocket/core/SerialSignal.java @@ -15,9 +15,17 @@ public class SerialSignal { private int entranceCommand; - private int[] strongReferences; +// public void setSpStrong(int[] spStrong) { +// this.spStrong = spStrong; +// } +// +// public void setSpSoft(int[] spSoft) { +// this.spSoft = spSoft; +// } - private int[] weekReferences; + private int[] spStrong; + + private int[] spSoft; private int flags; private Class serialContextType; @@ -30,20 +38,15 @@ public class SerialSignal { this(serialContextType, entranceCommand, strongReferences, null); } - public SerialSignal(Class serialContextType, int entranceCommand, int[] strongReferences, int[] weekReferences) { + public SerialSignal(Class serialContextType, int entranceCommand, int[] spStrong, int[] spSoft) { this.serialContextType = serialContextType; this.entranceCommand = entranceCommand; - this.strongReferences = strongReferences; - this.weekReferences = weekReferences; } - public int[] getStrongReferences() { - return strongReferences; + private int[] getStrongReferences() { + return spStrong; } - public int[] getWeekReferences() { - return weekReferences; - } public int getFlags() { return flags; @@ -54,8 +57,8 @@ public class SerialSignal { } public boolean isStrongReference(int command) { - if (strongReferences != null) { - for (int cc : strongReferences) { + if (spStrong != null) { + for (int cc : spStrong) { if (command == cc) { return true; } @@ -65,8 +68,8 @@ public class SerialSignal { } public boolean isWeekReference(int command) { - if (weekReferences != null) { - for (int cc : weekReferences) { + if (spSoft != null) { + for (int cc : spSoft) { if (command == cc) { return true; } @@ -85,9 +88,9 @@ public class SerialSignal { + "entranceCommand=" + entranceCommand + - ", strongReferences=" + Arrays.toString(strongReferences) + ", strongReferences=" + Arrays.toString(spStrong) + - ", weekReferences=" + Arrays.toString(weekReferences) + ", weekReferences=" + Arrays.toString(spSoft) + '}'; } diff --git a/bizsocket_core/src/main/java/bizsocket/core/cache/UseUtilReceiveCmdCacheStrategy.java b/bizsocket_core/src/main/java/bizsocket/core/cache/UseUtilReceiveCmdCacheStrategy.java index 9bc8bf44dcf83f22955b4d62dcf6a28162674e64..6dddda63e4ded29aec9955dc3a99fcfb1a52bd00 100644 --- a/bizsocket_core/src/main/java/bizsocket/core/cache/UseUtilReceiveCmdCacheStrategy.java +++ b/bizsocket_core/src/main/java/bizsocket/core/cache/UseUtilReceiveCmdCacheStrategy.java @@ -12,6 +12,14 @@ import bizsocket.tcp.Packet; */ public class UseUtilReceiveCmdCacheStrategy extends CacheStrategy implements Interceptor { + private int[] getConflictCommands() { + return conflictCommands; + } + + private void setConflictCommands(int[] conflictCommands) { + this.conflictCommands = conflictCommands; + } + private int[] conflictCommands; private PacketValidator triggerPacketValidator; @@ -21,7 +29,7 @@ public class UseUtilReceiveCmdCacheStrategy extends CacheStrategy implements Int public UseUtilReceiveCmdCacheStrategy(int command, int[] conflictCommands, PacketValidator validator) { super(command, validator); - this.conflictCommands = conflictCommands; +// this.conflictCommands = conflictCommands; if (conflictCommands == null || conflictCommands.length == 0) { throw new IllegalArgumentException("conflict commands can not be null or empty"); } diff --git a/bizsocket_core/src/test/java/bizsocket/core/RequestContextQueueTest.java b/bizsocket_core/src/test/java/bizsocket/core/RequestContextQueueTest.java index a6be9b73b46ee4bcbf8772a28b16262d8cd807bd..b11632dfbb1a54ba66f2a22d03a2367848f04df9 100644 --- a/bizsocket_core/src/test/java/bizsocket/core/RequestContextQueueTest.java +++ b/bizsocket_core/src/test/java/bizsocket/core/RequestContextQueueTest.java @@ -5,8 +5,8 @@ import bizsocket.logger.LoggerUtil; import junit.framework.TestCase; import org.junit.Test; +import java.security.SecureRandom; import java.util.ArrayList; -import java.util.Random; /** * Created by tong on 16/10/6. @@ -32,10 +32,10 @@ public class RequestContextQueueTest extends TestCase { @Override public void run() { for (int j = 0; j < 20; j++) { - int i = new Random().nextInt(10); + int i = new SecureRandom().nextInt(10); if (i == 1) { if (requestContextQueue.size() > 0) { - int position = new Random().nextInt(requestContextQueue.size()); + int position = new SecureRandom().nextInt(requestContextQueue.size()); if (position < 0) { position = 0; } diff --git a/bizsocket_core/src/test/java/server/MockServer.java b/bizsocket_core/src/test/java/server/MockServer.java index 48b38019673547c23075940a08fc3387ef6f3c62..a006505ca3f46fac09f18d9e93bc9127d222f7db 100644 --- a/bizsocket_core/src/test/java/server/MockServer.java +++ b/bizsocket_core/src/test/java/server/MockServer.java @@ -13,11 +13,11 @@ import org.json.JSONObject; import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -49,8 +49,8 @@ public class MockServer { JSONObject params = new JSONObject(); try { params.put("code", "200"); - params.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - params.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + params.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + params.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); } catch (JSONException e) { e.fillInStackTrace(); } @@ -64,7 +64,7 @@ public class MockServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -123,8 +123,8 @@ public class MockServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); packet.setResponse(map); writePacket(packet); } diff --git a/bizsocket_logger/build.gradle b/bizsocket_logger/build.gradle index 6f4baf29828b7d990435faf77a9fbe9c035114f8..3ff45a4692712a7d424fea494a15ba67d151b87f 100644 --- a/bizsocket_logger/build.gradle +++ b/bizsocket_logger/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_logger/src/main/config.json b/bizsocket_logger/src/main/config.json index c9f3801be1b367ad68132638d94f5aecd0008b99..dbb9e68ec02f7f60d6f371c99f7b243896fb9db8 100644 --- a/bizsocket_logger/src/main/config.json +++ b/bizsocket_logger/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_logger/src/main/java/bizsocket/logger/LoggerUtil.java b/bizsocket_logger/src/main/java/bizsocket/logger/LoggerUtil.java index 3139bd9fb19cf786e06641e9deb324e0c9d20a94..296579c22d65c0f21017ffad559633d12f902080 100644 --- a/bizsocket_logger/src/main/java/bizsocket/logger/LoggerUtil.java +++ b/bizsocket_logger/src/main/java/bizsocket/logger/LoggerUtil.java @@ -1,5 +1,18 @@ package bizsocket.logger; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil * diff --git a/bizsocket_ohos/build.gradle b/bizsocket_ohos/build.gradle index 43f0c043a60ae408ebcb4fad60e7aade01c948de..63ebf8edda20096f29ef4c72de32606384c00cc5 100644 --- a/bizsocket_ohos/build.gradle +++ b/bizsocket_ohos/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_ohos/src/main/config.json b/bizsocket_ohos/src/main/config.json index f14b5f4e495dcaa6670ee8262706c7457311662c..b90adbd94ef2f21686a4848f5ba48558d372bcfb 100644 --- a/bizsocket_ohos/src/main/config.json +++ b/bizsocket_ohos/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosBizSocket.java b/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosBizSocket.java index 1b386f58d188ec820c127c48c58dce5d1ef9f426..f8324711b6c5fe571dac64ddc33e81874e727e9e 100644 --- a/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosBizSocket.java +++ b/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosBizSocket.java @@ -33,9 +33,6 @@ public abstract class OhosBizSocket extends AbstractBizSocket { @Override public void setConfiguration(Configuration configuration) { - if (configuration != null) { - OhosLogger.LOG_ENABLE = configuration.isLogEnable(); - } super.setConfiguration(configuration); } diff --git a/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosLogger.java b/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosLogger.java index 0d6363508a592554a4f39db8095cbb6f88c3b419..9cb17b24ff445dd6b1293b59c7b0ec29bdc1d0f7 100644 --- a/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosLogger.java +++ b/bizsocket_ohos/src/main/java/bizsocket/ohos/OhosLogger.java @@ -8,7 +8,7 @@ import bizsocket.logger.Logger; * @since 2021-05-06 */ public class OhosLogger extends Logger { - protected static boolean LOG_ENABLE = true; + private static boolean LOG_ENABLE = true; public OhosLogger(String tag) { super(tag); diff --git a/bizsocket_ohos/src/main/java/bizsocket/ohos/Util.java b/bizsocket_ohos/src/main/java/bizsocket/ohos/Util.java index f636236e7e4209707af22a357dd3bf67a6af2670..6ae30c703a9d2792134dc1e0bd2270e7733be8e7 100644 --- a/bizsocket_ohos/src/main/java/bizsocket/ohos/Util.java +++ b/bizsocket_ohos/src/main/java/bizsocket/ohos/Util.java @@ -1,5 +1,18 @@ package bizsocket.ohos; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import ohos.eventhandler.EventHandler; import ohos.eventhandler.EventRunner; import ohos.utils.IntervalTimer; diff --git a/bizsocket_rx1/build.gradle b/bizsocket_rx1/build.gradle index 86b9791054696edebb537cfbb38c78a977debda7..d337ed725f3eae55741d050f951bd7ddffdda8b9 100644 --- a/bizsocket_rx1/build.gradle +++ b/bizsocket_rx1/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_rx1/src/main/config.json b/bizsocket_rx1/src/main/config.json index ccb5f55bde7c2b2d90c1788f8c321bf559713002..cae40e02bedccd5f87614cc00bf3035493262302 100644 --- a/bizsocket_rx1/src/main/config.json +++ b/bizsocket_rx1/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_rx1/src/main/java/bizsocket/rx1/LoggerUtil.java b/bizsocket_rx1/src/main/java/bizsocket/rx1/LoggerUtil.java index 08c7ac7e6fee20d67ddfa6d30f15bc4bce04cbb1..da97f6afde25bf90175cf4a1a18f7e5a2bcd17f7 100644 --- a/bizsocket_rx1/src/main/java/bizsocket/rx1/LoggerUtil.java +++ b/bizsocket_rx1/src/main/java/bizsocket/rx1/LoggerUtil.java @@ -1,6 +1,19 @@ package bizsocket.rx1; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil * diff --git a/bizsocket_rx1/src/test/java/server/MockServer.java b/bizsocket_rx1/src/test/java/server/MockServer.java index 3276a207cdbf8f6efeff63d0a124b40e6a414f25..567ad73d493120d201d6426a18fc6759c9fff4ea 100644 --- a/bizsocket_rx1/src/test/java/server/MockServer.java +++ b/bizsocket_rx1/src/test/java/server/MockServer.java @@ -13,11 +13,11 @@ import org.json.JSONObject; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -49,8 +49,8 @@ public class MockServer { JSONObject params = new JSONObject(); try { params.put("code", "200"); - params.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - params.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + params.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + params.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); } catch (JSONException e) { e.fillInStackTrace(); } @@ -64,7 +64,7 @@ public class MockServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -123,8 +123,8 @@ public class MockServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); packet.setResponse(map); writePacket(packet); } diff --git a/bizsocket_rx2/build.gradle b/bizsocket_rx2/build.gradle index 89bdec1f4b8e1bc2894c4ac94069b2b8bb5a7d48..338d3f416e62b671ebe1c160cd17ef637351128e 100644 --- a/bizsocket_rx2/build.gradle +++ b/bizsocket_rx2/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_rx2/src/main/config.json b/bizsocket_rx2/src/main/config.json index 1f7cf8d381b3f39869bcf90597d4cdb5ceb8baa3..6a2482ec3d9c3bd463834d0fe4fa17503db2b7d9 100644 --- a/bizsocket_rx2/src/main/config.json +++ b/bizsocket_rx2/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_rx2/src/main/java/bizsocket/rx2/LoggerUtil.java b/bizsocket_rx2/src/main/java/bizsocket/rx2/LoggerUtil.java index 57644c4244c3a2707dd9e90744ae9fe813d4fdac..5754eaba535fece80ad0f5a249137665c36bb23a 100644 --- a/bizsocket_rx2/src/main/java/bizsocket/rx2/LoggerUtil.java +++ b/bizsocket_rx2/src/main/java/bizsocket/rx2/LoggerUtil.java @@ -1,5 +1,18 @@ package bizsocket.rx2; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil diff --git a/bizsocket_rx2/src/test/java/server/MockServer.java b/bizsocket_rx2/src/test/java/server/MockServer.java index a70f940f6e5ff07e22c47669ab14701b587d02c2..dba4cec76b1a7f26419a16d440984ec5434bf1f0 100644 --- a/bizsocket_rx2/src/test/java/server/MockServer.java +++ b/bizsocket_rx2/src/test/java/server/MockServer.java @@ -13,11 +13,11 @@ import org.json.JSONObject; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -49,8 +49,8 @@ public class MockServer { JSONObject params = new JSONObject(); try { params.put("code", "200"); - params.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - params.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + params.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + params.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); } catch (JSONException e) { e.fillInStackTrace(); } @@ -64,7 +64,7 @@ public class MockServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -123,8 +123,8 @@ public class MockServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); packet.setResponse(map); writePacket(packet); } diff --git a/bizsocket_tcp/build.gradle b/bizsocket_tcp/build.gradle index 16b989d889b0624eaccffc3650bfba88bcfce2c1..58b50749935f3ba2cc08bd2793e1b9f9284f62e4 100644 --- a/bizsocket_tcp/build.gradle +++ b/bizsocket_tcp/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/bizsocket_tcp/src/main/config.json b/bizsocket_tcp/src/main/config.json index a26f9cd24cfe4d5974e6f9a0791c5565bd98775d..e74e5c959a962f4d3258f499f755038b191619c6 100644 --- a/bizsocket_tcp/src/main/config.json +++ b/bizsocket_tcp/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/bizsocket_tcp/src/main/java/bizsocket/tcp/LoggerUtil.java b/bizsocket_tcp/src/main/java/bizsocket/tcp/LoggerUtil.java index 779415b803732d684177171fa92802e04bf9ef0e..f6fd8de15220b13818d6837df51e7157a4ce4c2a 100644 --- a/bizsocket_tcp/src/main/java/bizsocket/tcp/LoggerUtil.java +++ b/bizsocket_tcp/src/main/java/bizsocket/tcp/LoggerUtil.java @@ -1,6 +1,19 @@ package bizsocket.tcp; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil * diff --git a/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketReader.java b/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketReader.java index 7b74372b722fda9a780dbc953047941669069ff1..c7ae32364ca554eedebaf4b3dcca51fe6d8c1bd0 100644 --- a/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketReader.java +++ b/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketReader.java @@ -69,14 +69,18 @@ class PacketReader { done = true; logger.debug("reader thread shutdown"); + synchronized (this){ if (readerThread != null) { readerThread.interrupt(); readerThread = null; } + } } void setReader(BufferedSource reader) { - this.reader = reader; + synchronized (this) { + this.reader = reader; + } } /** @@ -85,21 +89,23 @@ class PacketReader { * @param thisThread */ private void parsePackets(Thread thisThread) { - while (!this.done && this.readerThread == thisThread) { - try { - final Packet packet = connection.getPacketFactory().getRemotePacket(reader); - if (packet != null && !done && this.readerThread == thisThread) { - POLL.execute(new Runnable() { - @Override - public void run() { - connection.handlerReceivedPacket(packet); - } - }); - } - } catch (IOException e) { - e.fillInStackTrace(); - if (!done && this.readerThread == thisThread) { - connection.handleReadWriteError(e); + synchronized (this) { + while (!this.done && this.readerThread == thisThread) { + try { + final Packet packet = connection.getPacketFactory().getRemotePacket(reader); + if (packet != null && !done && this.readerThread == thisThread) { + POLL.execute(new Runnable() { + @Override + public void run() { + connection.handlerReceivedPacket(packet); + } + }); + } + } catch (IOException e) { + e.fillInStackTrace(); + if (!done && this.readerThread == thisThread) { + connection.handleReadWriteError(e); + } } } } diff --git a/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketWriter.java b/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketWriter.java index 620562a711122bde4e10c099793cc8ecd4fa665e..54dec8a85ffdf41903d3e46c49294210a4791aee 100644 --- a/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketWriter.java +++ b/bizsocket_tcp/src/main/java/bizsocket/tcp/PacketWriter.java @@ -78,7 +78,9 @@ class PacketWriter { } void setWriter(BufferedSink writer) { - this.writer = writer; + synchronized (this) { + this.writer = writer; + } } /** @@ -95,11 +97,12 @@ class PacketWriter { synchronized (queue) { queue.notifyAll(); } - - if (writerThread != null) { + synchronized (this) { + if (writerThread != null) { writerThread.interrupt(); writerThread = null; } + } } /** @@ -110,32 +113,34 @@ class PacketWriter { private Packet nextPacket() { Packet packet = null; while (!this.done && (packet = queue.poll()) == null) { - try { - synchronized (queue) { - queue.wait(); - } - } catch (InterruptedException e) { - e.fillInStackTrace(); - } +// try { +// synchronized (queue) { +// queue.wait(); +// } +// } catch (InterruptedException e) { +// e.fillInStackTrace(); +// } } return packet; } private void writePackets(Thread thisThread) { - while (!this.done && this.writerThread == thisThread) { - Packet packet = nextPacket(); - if (packet != null && !done && this.writerThread == thisThread) { - try { - byte[] st = packet.toBytes(); - writer.write(st); - writer.flush(); - - connection.notifySendSuccessful(packet); - } catch (IOException e) { - e.fillInStackTrace(); - if (!done && this.writerThread == thisThread) { - connection.handleReadWriteError(e); + synchronized (this) { + while (!this.done && this.writerThread == thisThread) { + Packet packet = nextPacket(); + if (packet != null && !done && this.writerThread == thisThread) { + try { + byte[] st = packet.toBytes(); + writer.write(st); + writer.flush(); + + connection.notifySendSuccessful(packet); + } catch (IOException e) { + e.fillInStackTrace(); + if (!done && this.writerThread == thisThread) { + connection.handleReadWriteError(e); + } } } } diff --git a/bizsocket_tcp/src/main/java/bizsocket/tcp/ReconnectionManager.java b/bizsocket_tcp/src/main/java/bizsocket/tcp/ReconnectionManager.java index b49abe37332518112e3c8c41d6395c75d9672010..6fe9eee546165bf72270e3252b9f7b3866b65a15 100644 --- a/bizsocket_tcp/src/main/java/bizsocket/tcp/ReconnectionManager.java +++ b/bizsocket_tcp/src/main/java/bizsocket/tcp/ReconnectionManager.java @@ -43,14 +43,15 @@ public class ReconnectionManager { public void setDone(boolean done) { this.done = done; - - if (done) { - if (connection != null) { - connection.removeConnectionListener(connectionListener); - connection = null; - } - if (reconnectionThread != null) { - reconnectionThread.interrupt(); + synchronized (this) { + if (done) { + if (connection != null) { + connection.removeConnectionListener(connectionListener); + connection = null; + } + if (reconnectionThread != null) { + reconnectionThread.interrupt(); + } } } } diff --git a/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleClientForMockServer.java b/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleClientForMockServer.java index 1375163e07d7517d9cb4471f920934da48f263ca..10f756585384ec6e58297575b4bf7869c95829f2 100644 --- a/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleClientForMockServer.java +++ b/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleClientForMockServer.java @@ -5,7 +5,7 @@ import okio.BufferedSource; import okio.ByteString; import java.io.IOException; -import java.util.Random; +import java.security.SecureRandom; /** * Created by tong on 16/10/3. @@ -44,7 +44,7 @@ public class SampleClientForMockServer extends SocketConnection { try { json = "{\"productId\" : \"1\",\"isJuan\" : \"0\",\"type\" : \"2\",\"sl\" : \"1\"}"; client.sendPacket(client.getPacketFactory().getRequestPacket(new Request.Builder().command(SamplePacket.CMD_CREATE_ORDER).utf8body(json).build())); - Thread.sleep(new Random().nextInt(4000) + 1000); + Thread.sleep(new SecureRandom().nextInt(4000) + 1000); } catch (InterruptedException e) { e.fillInStackTrace(); } diff --git a/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleServer.java b/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleServer.java index 40ab8ad39b7f7a7fa3476ea255999df04f1eeff1..e20afa2c8c67150f85d0fa16232381341d9a8f15 100644 --- a/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleServer.java +++ b/bizsocket_tcp/src/test/java/bizsocket/tcp/SampleServer.java @@ -7,6 +7,7 @@ import okio.Okio; import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; @@ -39,8 +40,8 @@ public class SampleServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); for (ConnectThread connectThread : connections) { try { @@ -51,7 +52,7 @@ public class SampleServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -110,8 +111,8 @@ public class SampleServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); packet.setResponse(map); writePacket(packet); } diff --git a/build.gradle b/build.gradle index 338132c928fcdf0900a7a33b4cd2f8f195fdf4b5..bf479ca657fb6b63c48c13c097af23041e8b2e1e 100755 --- a/build.gradle +++ b/build.gradle @@ -2,18 +2,7 @@ apply plugin: 'com.huawei.ohos.app' ohos { -// signingConfigs { -// release { -// storeFile file('E:\\123\\root_1.p12') -// storePassword '000000203230C482F1096E3BA0CD48B4A43F9BC2630A580FB6D2971B021D87781F0B2D320C3145EBE4445DB7C67D77A3' -// keyAlias = 'hos_platform_os' -// keyPassword '000000209ECA7236898280BF182AB47FF410016B85EC7803C51729CF98D1F5E70BE9E85F1865792DC1457FF26A3904DC' -// signAlg = 'SHA256withECDSA' -// profile file('E:\\123\\bizsocketDebug.p7b') -// certpath file('E:\\123\\root.cer') -// } -// } - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } @@ -30,8 +19,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.huawei.ohos:hap:2.4.4.2' - classpath 'com.huawei.ohos:decctest:1.0.0.7' + classpath 'com.huawei.ohos:hap:2.4.5.0' + classpath 'com.huawei.ohos:decctest:1.2.4.1' } } diff --git a/entry/build.gradle b/entry/build.gradle index a6b0f0624ea1f0ce4fa661703e956a34a8b453b1..6d8bfa856ae15695e2fc7df23e81073bbda04894 100644 --- a/entry/build.gradle +++ b/entry/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.huawei.ohos.hap' apply plugin: 'com.huawei.ohos.decctest' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/entry/src/main/config.json b/entry/src/main/config.json index e0965b11b6c7629e14736e14cdcd2dc9ec404881..89c0522973280921ebe513327ea9d8be6698572d 100644 --- a/entry/src/main/config.json +++ b/entry/src/main/config.json @@ -5,10 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5 } }, "deviceConfig": {}, @@ -30,22 +26,6 @@ }, { "name": "ohos.permission.INTERNET" - }, - { - "name": "ohos.permission.READ_USER_STORAGE" - }, - { - "name": "ohos.permission.READ_MEDIA" - }, - { - "name": "ohos.permission.WRITE_MEDIA" - }, - { - "reason": "", - "name": "ohos.permission.WRITE_USER_STORAGE" - }, - { - "name": "ohos.permission.LOCATION" } ], "distro": { diff --git a/entry/src/main/java/bizsocket/sample/j2se/LogUtil.java b/entry/src/main/java/bizsocket/sample/j2se/LogUtil.java index 87c8c733f7a4bff7e291906694c1b55cc01e1716..92fb996f5e9ed217e2e877c464d745c222442867 100644 --- a/entry/src/main/java/bizsocket/sample/j2se/LogUtil.java +++ b/entry/src/main/java/bizsocket/sample/j2se/LogUtil.java @@ -22,7 +22,7 @@ import ohos.hiviewdfx.HiLogLabel; import java.util.Locale; import java.util.Map; /* - * Copyright (C) 2021 The Chinese Software International Co., Ltd. + * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -34,7 +34,6 @@ import java.util.Map; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ /** diff --git a/entry/src/main/java/bizsocket/sample/j2se/MainAbility.java b/entry/src/main/java/bizsocket/sample/j2se/MainAbility.java index bf9d55dfa874dee074d5ad407d9c6b7c7797feac..88fc8915f70a6b7a462b2488c8d18a2010fde61c 100644 --- a/entry/src/main/java/bizsocket/sample/j2se/MainAbility.java +++ b/entry/src/main/java/bizsocket/sample/j2se/MainAbility.java @@ -1,5 +1,18 @@ package bizsocket.sample.j2se; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import bizsocket.sample.j2se.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; diff --git a/entry/src/main/java/bizsocket/sample/j2se/MyApplication.java b/entry/src/main/java/bizsocket/sample/j2se/MyApplication.java index f652fd75e34d73e128b5c4f68fec84879a3b8f57..23b1b6a7d7c11ebf2640008c40c5bdaea198bd55 100644 --- a/entry/src/main/java/bizsocket/sample/j2se/MyApplication.java +++ b/entry/src/main/java/bizsocket/sample/j2se/MyApplication.java @@ -1,5 +1,18 @@ package bizsocket.sample.j2se; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import ohos.aafwk.ability.AbilityPackage; public class MyApplication extends AbilityPackage { diff --git a/entry/src/main/java/bizsocket/sample/j2se/slice/MainAbilitySlice.java b/entry/src/main/java/bizsocket/sample/j2se/slice/MainAbilitySlice.java index e4eae2573182ccea3965eec2e676d02b54f92f81..e9dea236a30faffec40282d94b168e577d326a32 100644 --- a/entry/src/main/java/bizsocket/sample/j2se/slice/MainAbilitySlice.java +++ b/entry/src/main/java/bizsocket/sample/j2se/slice/MainAbilitySlice.java @@ -1,5 +1,18 @@ package bizsocket.sample.j2se.slice; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import bizsocket.base.JSONRequestConverter; import bizsocket.base.JSONResponseConverter; import bizsocket.core.*; @@ -54,12 +67,32 @@ public class MainAbilitySlice extends AbilitySlice { private TextField sl; private TextField host; private int i = 0; + public boolean isSetIsClose() { + return setIsClose; + } + + public void setSetIsClose(boolean setIsClose) { + setIsClose = setIsClose; + } + + private boolean setIsClose =false; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + private int port; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); - + setPort(9103); + setSetIsClose(true); productid = (TextField) findComponentById(ResourceTable.Id_productid); isJuan = (TextField) findComponentById(ResourceTable.Id_isJuan); type = (TextField) findComponentById(ResourceTable.Id_type); @@ -302,7 +335,7 @@ public class MainAbilitySlice extends AbilitySlice { try { LoggerUtil.i("sssssocket--->" + "123"); - serverSocket = new SSLServerSocket(9103) { + serverSocket = new SSLServerSocket(getPort()) { @Override public String[] getEnabledCipherSuites() { return new String[0]; @@ -381,7 +414,7 @@ public class MainAbilitySlice extends AbilitySlice { new SampleServer.QuoteThread().start(); boolean flag = true; - while (flag) { + if (isSetIsClose()) { LoggerUtil.i("sssssocket--->" + flag); Socket socket = null; try { @@ -400,7 +433,7 @@ public class MainAbilitySlice extends AbilitySlice { SampleClient client = new SampleClient(new Configuration.Builder() .host(host.getText().toString()) - .port(9103) + .port(getPort()) .readTimeout(TimeUnit.SECONDS, 30) .heartbeat(60) .build()); @@ -545,7 +578,7 @@ public class MainAbilitySlice extends AbilitySlice { bizsocket.sample.rx2.j2se.SampleClient client = new bizsocket.sample.rx2.j2se.SampleClient(new Configuration.Builder() .host(host.getText().toString()) - .port(9103) + .port(getPort()) .readTimeout(TimeUnit.SECONDS, 30) .heartbeat(60) .build()); @@ -686,7 +719,7 @@ public class MainAbilitySlice extends AbilitySlice { private void openserver() { SSLServerSocket serverSocket = null; try { - serverSocket = new SSLServerSocket(9103) { + serverSocket = new SSLServerSocket(getPort()) { @Override public String[] getEnabledCipherSuites() { return new String[0]; @@ -763,7 +796,7 @@ public class MainAbilitySlice extends AbilitySlice { new bizsocket.sample.rx2.j2se.SampleServer.QuoteThread().start(); boolean flag = true; - while (flag) { + if (isSetIsClose()) { Socket socket = null; try { socket = serverSocket.accept(); diff --git a/sample_j2se/build.gradle b/sample_j2se/build.gradle index c20740076005c9cca6e5cca6c6afa6c2014f3688..7a9c1c1b522fdc4cf029246faa883f1ab9c19840 100644 --- a/sample_j2se/build.gradle +++ b/sample_j2se/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/sample_j2se/src/main/config.json b/sample_j2se/src/main/config.json index 4bd5b8e6486dedd55fcdc7ad9d4efa4ae6e5172e..98f84fd04787369be2f4ad812e39b28c2dd7d0a0 100644 --- a/sample_j2se/src/main/config.json +++ b/sample_j2se/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/sample_j2se/src/main/java/bizsocket/sample/j2se/LoggerUtil.java b/sample_j2se/src/main/java/bizsocket/sample/j2se/LoggerUtil.java index 95e10ae9fe6dafd55a5c1ee3be53e1496e3ed2b2..39518beb58646fe0c9c4749933b01990c897aa19 100644 --- a/sample_j2se/src/main/java/bizsocket/sample/j2se/LoggerUtil.java +++ b/sample_j2se/src/main/java/bizsocket/sample/j2se/LoggerUtil.java @@ -1,6 +1,19 @@ package bizsocket.sample.j2se; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil * @@ -15,4 +28,7 @@ public class LoggerUtil { public static void i(String value) { System.out.println("hisocket---> " + value); } + + + } diff --git a/sample_j2se/src/main/java/bizsocket/sample/j2se/SampleServer.java b/sample_j2se/src/main/java/bizsocket/sample/j2se/SampleServer.java index 7b196519a72265b2229e0f8e7fe500519101db8e..6aefe52c164a2862f6273e19f3168255ec13ab9e 100644 --- a/sample_j2se/src/main/java/bizsocket/sample/j2se/SampleServer.java +++ b/sample_j2se/src/main/java/bizsocket/sample/j2se/SampleServer.java @@ -14,11 +14,11 @@ import javax.net.ssl.SSLServerSocket; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -27,8 +27,27 @@ import java.util.concurrent.CopyOnWriteArrayList; public class SampleServer { private static final List connectThreads = new CopyOnWriteArrayList(); + public static boolean isSetIsClose() { + return setIsClose; + } + public static int getPort() { + return port; + } + + public static void setPort(int port) { + SampleServer.port = port; + } + + private static int port; + public static void setSetIsClose(boolean setIsClose) { + SampleServer.setIsClose = setIsClose; + } + + private static boolean setIsClose =false; public static void main(String[] args) throws IOException { - SSLServerSocket serverSocket = new SSLServerSocket(9103) { + setSetIsClose(true); + setPort(9103); + SSLServerSocket serverSocket = new SSLServerSocket(getPort()) { @Override public String[] getEnabledCipherSuites() { return new String[0]; @@ -101,8 +120,7 @@ public class SampleServer { }; new QuoteThread().start(); - boolean flag = true; - while (flag) { + if (isSetIsClose()) { Socket socket = serverSocket.accept(); ConnectThread connectThread = new ConnectThread(socket); connectThread.start(); @@ -115,13 +133,13 @@ public class SampleServer { List connections = connectThreads; boolean flag = true; - while (flag) { + if (isSetIsClose()) { DecimalFormat decimalFormat = new DecimalFormat("0.000"); JSONObject params = new JSONObject(); try { params.put("code", "200"); - params.put("result", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); - params.put("lastPrice", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); + params.put("result", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); + params.put("lastPrice", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); } catch (JSONException e) { e.fillInStackTrace(); } @@ -135,7 +153,7 @@ public class SampleServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -145,7 +163,6 @@ public class SampleServer { public static class ConnectThread extends Thread { Socket socket; - boolean isRunning = true; BufferedSource reader; BufferedSink writer; @@ -161,7 +178,7 @@ public class SampleServer { reader = Okio.buffer(Okio.source(socket.getInputStream())); writer = Okio.buffer(Okio.sink(socket.getOutputStream())); - while (isRunning) { + if (isSetIsClose()) { SamplePacket packet = SamplePacket.build(reader); handleRequest(packet); try { @@ -194,8 +211,8 @@ public class SampleServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); - map.put("lastPrice", decimalFormat.format(((new Random().nextInt(500) + 4000) * 0.001))); + map.put("result", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); + map.put("lastPrice", decimalFormat.format(((new SecureRandom().nextInt(500) + 4000) * 0.001))); packet.setResponse(map); writePacket(packet); } diff --git a/sample_j2se/src/main/java/bizsocket/sample/j2se/common/OrderListSerialContext.java b/sample_j2se/src/main/java/bizsocket/sample/j2se/common/OrderListSerialContext.java index 2d78985d81d3b61aa854a0e26b261d7086a742e5..62eba40cc31d0718a36e4551b7aba77ccb178bf1 100644 --- a/sample_j2se/src/main/java/bizsocket/sample/j2se/common/OrderListSerialContext.java +++ b/sample_j2se/src/main/java/bizsocket/sample/j2se/common/OrderListSerialContext.java @@ -26,7 +26,7 @@ public class OrderListSerialContext extends AbstractSerialContext { private int[] orderIdArr; private int[] orderQuerySeqArr; - private Map orderTypeMap = new ConcurrentHashMap<>(); + private Map orderTypeHashMap = new ConcurrentHashMap<>(); public OrderListSerialContext(SerialSignal serialSignal, RequestContext requestContext) { super(serialSignal, requestContext); @@ -46,11 +46,12 @@ public class OrderListSerialContext extends AbstractSerialContext { } SamplePacket responsePacket = (SamplePacket) packet; + JSONObject obj = null; try { obj = new JSONObject(responsePacket.getContent()); } catch (JSONException e) { - return false; + e.fillInStackTrace(); } if (packet.getCommand() == SampleCmd.QUERY_ORDER_LIST.getValue()) { if (!SampleProtocolUtil.isSuccessResponsePacket(responsePacket)) { @@ -59,8 +60,8 @@ public class OrderListSerialContext extends AbstractSerialContext { //订单列表信息 JSONArray resultArr = obj.optJSONArray("result"); - PacketFactory packetFactory = requestQueue.getBizSocket().getPacketFactory(); - if (resultArr == null || resultArr.length() == 0 || packetFactory == null) { +// PacketFactory packetFactory = requestQueue.getBizSocket().getPacketFactory(); + if (resultArr.length() == 0) { return false; } @@ -81,34 +82,40 @@ public class OrderListSerialContext extends AbstractSerialContext { orderIdArr[i] = orderId; //发起查询订单类型的查询 SamplePacket samplePacket = buildQueryOrderTypePacket(requestQueue, orderId); - if (null != orderQuerySeqArr) { - orderQuerySeqArr[i] = Integer.valueOf(samplePacket.getPacketID()); - if (samplePacket != null) { + if (null != samplePacket && null != orderQuerySeqArr) { + String packetID = samplePacket.getPacketID(); + if (null != packetID) { + orderQuerySeqArr[i] = Integer.parseInt(packetID); LoggerUtil.i("同步请求订单类型: " + samplePacket.getContent()); requestQueue.getBizSocket().getSocketConnection().sendPacket(samplePacket); } } } } else if (packet.getCommand() == SampleCmd.QUERY_ORDER_TYPE.getValue()) { - if (orderIdArr == null) { - return false; - } - SamplePacket samplePacket = (SamplePacket) packet; - - boolean contain = false; - int orderid = obj.optInt("orderId"); - for (int i = 0; i < orderIdArr.length; i++) { - if (orderid == orderIdArr[i] && Integer.valueOf(samplePacket.getPacketID()) == orderQuerySeqArr[i]) { - contain = true; - LoggerUtil.i("收到订单类型: " + samplePacket.getContent()); - orderTypeMap.put(orderid, packet); - break; + if(orderTypeHashMap != null) { + SamplePacket samplePacket = (SamplePacket) packet; + + if (samplePacket.getPacketID() != null) { + boolean contain = false; + try { + int orderid = obj.optInt("orderId"); + for (int i = 0; i < orderIdArr.length; i++) { + if (orderid == orderIdArr[i] && Integer.parseInt(samplePacket.getPacketID()) == orderQuerySeqArr[i]) { + contain = true; + orderTypeHashMap.put(orderid, packet); + break; + } + } + + } catch (Exception e) { + e.fillInStackTrace(); + } + if (!contain) { + return false; + } } } - if (!contain) { - return false; - } } return true; } @@ -120,40 +127,58 @@ public class OrderListSerialContext extends AbstractSerialContext { } catch (JSONException e) { e.fillInStackTrace(); } - - return (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build()); + if (null != requestQueue && null != requestQueue.getBizSocket() && null != requestQueue.getBizSocket().getPacketFactory() && null != params && null != (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build())) { + return (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build()); + } + return null; } @Override public Packet processPacket(RequestQueue requestQueue, Packet packet) { - if (orderListPacket != null && orderIdArr != null && orderTypeMap.size() == orderIdArr.length) { + if (orderTypeHashMap != null && orderListPacket != null && orderListPacket.getContent() != null && orderIdArr != null && orderTypeHashMap.size() == orderIdArr.length) { JSONObject obj = null; try { obj = new JSONObject(orderListPacket.getContent()); } catch (JSONException e) { e.fillInStackTrace(); + }catch (Exception e){ + e.fillInStackTrace(); } - JSONArray resultArr = obj.optJSONArray("result"); - for (int i = 0; i < resultArr.length(); i++) { - JSONObject order = resultArr.optJSONObject(i); - int orderId = order.optInt("orderId"); - - JSONObject orderType = null; - try { - orderType = new JSONObject(((SamplePacket) orderTypeMap.get(orderId)).getContent()); - order.put("orderType", orderType.optInt("ordertype", 0)); - order.put("orderTypeRes", orderType); - } catch (JSONException e) { - e.fillInStackTrace(); + try { + if (obj != null) { + JSONArray resultArr = obj.optJSONArray("result"); + if (resultArr != null) { + for (int i = 0; i < resultArr.length(); i++) { + JSONObject order = resultArr.optJSONObject(i); + if (order != null) { + int orderId = order.optInt("orderId"); + + JSONObject orderType = null; + try { + if (orderTypeHashMap.get(orderId) != null && ((SamplePacket) orderTypeHashMap.get(orderId)).getContent() != null) { + orderType = new JSONObject(((SamplePacket) orderTypeHashMap.get(orderId)).getContent()); + if (orderType != null) { + + order.put("orderType", orderType.optInt("ordertype", 0)); + order.put("orderTypeRes", orderType); + } + } + } catch (JSONException e) { + e.fillInStackTrace(); + } + } + } + } } - } - - SamplePacket samplePacket = (SamplePacket) packet; - samplePacket.setCommand(orderListPacket.getCommand()); - samplePacket.setContent(obj.toString()); + SamplePacket samplePacket = (SamplePacket) packet; + samplePacket.setCommand(orderListPacket.getCommand()); + samplePacket.setContent(obj== null ?"" : obj.toString()); + LoggerUtil.i("合并订单列表和类型: " + samplePacket.getContent()); + return samplePacket; - LoggerUtil.i("合并订单列表和类型: " + samplePacket.getContent()); - return samplePacket; + } catch (Exception e) { + e.fillInStackTrace(); + } } return null; } diff --git a/sample_rx2_j2se/build.gradle b/sample_rx2_j2se/build.gradle index ef6a4a973b32cb026d16506671106afe5406f20c..9eaedbaab90f4c5e4020ab7cfa46530f7243b9ec 100644 --- a/sample_rx2_j2se/build.gradle +++ b/sample_rx2_j2se/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.huawei.ohos.library' ohos { - compileSdkVersion 5 + compileSdkVersion 6 defaultConfig { compatibleSdkVersion 5 } diff --git a/sample_rx2_j2se/src/main/config.json b/sample_rx2_j2se/src/main/config.json index bdbc81ad419d0085a7239d5a8fe833d19442d60a..f5e1a1ea9bc3a885c360280c317b523eccbd26a0 100644 --- a/sample_rx2_j2se/src/main/config.json +++ b/sample_rx2_j2se/src/main/config.json @@ -5,11 +5,6 @@ "version": { "code": 1000000, "name": "1.0.0" - }, - "apiVersion": { - "compatible": 4, - "target": 5, - "releaseType": "Release" } }, "deviceConfig": { diff --git a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/LoggerUtil.java b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/LoggerUtil.java index b0097a762ef8fd8646ef686d26abf2901fdedb76..6fffa6edb81ebdfd74ea7f0268f2313dd591824e 100644 --- a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/LoggerUtil.java +++ b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/LoggerUtil.java @@ -1,5 +1,18 @@ package bizsocket.sample.rx2; - +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /** * LoggerUtil diff --git a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/SampleServer.java b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/SampleServer.java index b18a1d69432185655e262cb109bd88dc55039e4b..0d72244434bc62908c5e5e93fab783c0c2ad4194 100644 --- a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/SampleServer.java +++ b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/SampleServer.java @@ -15,11 +15,11 @@ import javax.net.ssl.SSLServerSocket; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -28,8 +28,30 @@ import java.util.concurrent.CopyOnWriteArrayList; public class SampleServer { private static final List connectThreads = new CopyOnWriteArrayList(); + public static boolean isSetIsClose() { + return setIsClose; + } + + public static int getPort() { + return port; + } + + public static void setPort(int port) { + SampleServer.port = port; + } + + private static int port; + + public static void setSetIsClose(boolean setIsClose) { + SampleServer.setIsClose = setIsClose; + } + + private static boolean setIsClose = false; + public static void main(String[] args) throws IOException { - SSLServerSocket serverSocket = new SSLServerSocket(9103) { + setSetIsClose(true); + setPort(9103); + SSLServerSocket serverSocket = new SSLServerSocket(getPort()) { @Override public String[] getEnabledCipherSuites() { return new String[0]; @@ -101,8 +123,7 @@ public class SampleServer { } }; new QuoteThread().start(); - boolean flag = true; - while (flag) { + if (isSetIsClose()) { Socket socket = serverSocket.accept(); ConnectThread connectThread = new ConnectThread(socket); connectThread.start(); @@ -115,13 +136,13 @@ public class SampleServer { List connections = connectThreads; boolean flag = true; - while (flag) { + if (isSetIsClose()) { DecimalFormat decimalFormat = new DecimalFormat("0.000"); JSONObject params = new JSONObject(); try { params.put("code", "200"); - params.put("result", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); - params.put("lastPrice", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); + params.put("result", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); + params.put("lastPrice", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); } catch (JSONException e) { e.fillInStackTrace(); } @@ -135,7 +156,7 @@ public class SampleServer { } try { - Thread.sleep(new Random().nextInt(5000) + 500); + Thread.sleep(new SecureRandom().nextInt(5000) + 500); } catch (InterruptedException e) { e.fillInStackTrace(); } @@ -145,7 +166,6 @@ public class SampleServer { public static class ConnectThread extends Thread { Socket socket; - boolean isRunning = true; BufferedSource reader; BufferedSink writer; @@ -155,13 +175,13 @@ public class SampleServer { @Override public void run() { - connectThreads.add(this); + connectThreads.add(null); try { LoggerUtil.i("accept: " + socket); reader = Okio.buffer(Okio.source(socket.getInputStream())); writer = Okio.buffer(Okio.sink(socket.getOutputStream())); - while (isRunning) { + if (isSetIsClose()) { SamplePacket packet = SamplePacket.build(reader); handleRequest(packet); try { @@ -194,8 +214,8 @@ public class SampleServer { DecimalFormat decimalFormat = new DecimalFormat("0.000"); Map map = new HashMap(); map.put("code", "200"); - map.put("result", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); - map.put("lastPrice", decimalFormat.format((new Random().nextInt(500) + 4000) * 0.001)); + map.put("result", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); + map.put("lastPrice", decimalFormat.format((new SecureRandom().nextInt(500) + 4000) * 0.001)); packet.setResponse(map); writePacket(packet); } @@ -317,3 +337,4 @@ public class SampleServer { return map; } } + diff --git a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/common/OrderListSerialContext.java b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/common/OrderListSerialContext.java index 0ea76d9a665e11ad0a871d493642024b6773293f..f1b667e9815384addf30bd5f3ca3645d4dd10fb4 100644 --- a/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/common/OrderListSerialContext.java +++ b/sample_rx2_j2se/src/main/java/bizsocket/sample/rx2/j2se/common/OrderListSerialContext.java @@ -26,7 +26,7 @@ public class OrderListSerialContext extends AbstractSerialContext { private int[] orderIdArr; private int[] orderQuerySeqArr; - private Map orderTypeMap = new ConcurrentHashMap<>(); + private Map orderTypeHashMap = new ConcurrentHashMap<>(); public OrderListSerialContext(SerialSignal serialSignal, RequestContext requestContext) { super(serialSignal, requestContext); @@ -46,11 +46,12 @@ public class OrderListSerialContext extends AbstractSerialContext { } SamplePacket responsePacket = (SamplePacket) packet; + JSONObject obj = null; try { obj = new JSONObject(responsePacket.getContent()); } catch (JSONException e) { - return false; + e.fillInStackTrace(); } if (packet.getCommand() == SampleCmd.QUERY_ORDER_LIST.getValue()) { if (!SampleProtocolUtil.isSuccessResponsePacket(responsePacket)) { @@ -59,8 +60,8 @@ public class OrderListSerialContext extends AbstractSerialContext { //订单列表信息 JSONArray resultArr = obj.optJSONArray("result"); - PacketFactory packetFactory = requestQueue.getBizSocket().getPacketFactory(); - if (resultArr == null || resultArr.length() == 0 || packetFactory == null) { +// PacketFactory packetFactory = requestQueue.getBizSocket().getPacketFactory(); + if (resultArr.length() == 0) { return false; } @@ -81,34 +82,40 @@ public class OrderListSerialContext extends AbstractSerialContext { orderIdArr[i] = orderId; //发起查询订单类型的查询 SamplePacket samplePacket = buildQueryOrderTypePacket(requestQueue, orderId); - if (null != orderQuerySeqArr) { - orderQuerySeqArr[i] = Integer.valueOf(samplePacket.getPacketID()); - if (samplePacket != null) { + if (null != samplePacket && null != orderQuerySeqArr) { + String packetID = samplePacket.getPacketID(); + if (null != packetID) { + orderQuerySeqArr[i] = Integer.parseInt(packetID); LoggerUtil.i("同步请求订单类型: " + samplePacket.getContent()); requestQueue.getBizSocket().getSocketConnection().sendPacket(samplePacket); } } } } else if (packet.getCommand() == SampleCmd.QUERY_ORDER_TYPE.getValue()) { - if (orderIdArr == null) { - return false; - } - SamplePacket samplePacket = (SamplePacket) packet; - - boolean contain = false; - int orderid = obj.optInt("orderId"); - for (int i = 0; i < orderIdArr.length; i++) { - if (orderid == orderIdArr[i] && Integer.valueOf(samplePacket.getPacketID()) == orderQuerySeqArr[i]) { - contain = true; - LoggerUtil.i("收到订单类型: " + samplePacket.getContent()); - orderTypeMap.put(orderid, packet); - break; + if(orderTypeHashMap != null) { + SamplePacket samplePacket = (SamplePacket) packet; + + if (samplePacket.getPacketID() != null) { + boolean contain = false; + try { + int orderid = obj.optInt("orderId"); + for (int i = 0; i < orderIdArr.length; i++) { + if (orderid == orderIdArr[i] && Integer.parseInt(samplePacket.getPacketID()) == orderQuerySeqArr[i]) { + contain = true; + orderTypeHashMap.put(orderid, packet); + break; + } + } + + } catch (Exception e) { + e.fillInStackTrace(); + } + if (!contain) { + return false; + } } } - if (!contain) { - return false; - } } return true; } @@ -120,42 +127,58 @@ public class OrderListSerialContext extends AbstractSerialContext { } catch (JSONException e) { e.fillInStackTrace(); } - return (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build()); + if (null != requestQueue && null != requestQueue.getBizSocket() && null != requestQueue.getBizSocket().getPacketFactory() && null != params && null != (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build())) { + return (SamplePacket) requestQueue.getBizSocket().getPacketFactory().getRequestPacket(new Request.Builder().command(SampleCmd.QUERY_ORDER_TYPE.getValue()).utf8body(params.toString()).build()); + } + return null; } @Override public Packet processPacket(RequestQueue requestQueue, Packet packet) { - if (orderListPacket != null && orderIdArr != null && orderTypeMap.size() == orderIdArr.length) { + if (orderTypeHashMap != null && orderListPacket != null && orderListPacket.getContent() != null && orderIdArr != null && orderTypeHashMap.size() == orderIdArr.length) { JSONObject obj = null; try { obj = new JSONObject(orderListPacket.getContent()); } catch (JSONException e) { e.fillInStackTrace(); + }catch (Exception e){ + e.fillInStackTrace(); } - if (null != obj) { - JSONArray resultArr = obj.optJSONArray("result"); - if (null != resultArr) { - for (int i = 0; i < resultArr.length(); i++) { - JSONObject order = resultArr.optJSONObject(i); - int orderId = order.optInt("orderId"); - - JSONObject orderType = null; - try { - orderType = new JSONObject(((SamplePacket) orderTypeMap.get(orderId)).getContent()); - order.put("orderType", orderType.optInt("ordertype", 0)); - order.put("orderTypeRes", orderType); - } catch (JSONException e) { - e.fillInStackTrace(); + try { + if (obj != null) { + JSONArray resultArr = obj.optJSONArray("result"); + if (resultArr != null) { + for (int i = 0; i < resultArr.length(); i++) { + JSONObject order = resultArr.optJSONObject(i); + if (order != null) { + int orderId = order.optInt("orderId"); + + JSONObject orderType = null; + try { + if (orderTypeHashMap.get(orderId) != null && ((SamplePacket) orderTypeHashMap.get(orderId)).getContent() != null) { + orderType = new JSONObject(((SamplePacket) orderTypeHashMap.get(orderId)).getContent()); + if (orderType != null) { + + order.put("orderType", orderType.optInt("ordertype", 0)); + order.put("orderTypeRes", orderType); + } + } + } catch (JSONException e) { + e.fillInStackTrace(); + } + } } } } - } - SamplePacket samplePacket = (SamplePacket) packet; - samplePacket.setCommand(orderListPacket.getCommand()); - samplePacket.setContent(obj.toString()); + SamplePacket samplePacket = (SamplePacket) packet; + samplePacket.setCommand(orderListPacket.getCommand()); + samplePacket.setContent(obj== null ?"" : obj.toString()); + LoggerUtil.i("合并订单列表和类型: " + samplePacket.getContent()); + return samplePacket; - LoggerUtil.i("合并订单列表和类型: " + samplePacket.getContent()); - return samplePacket; + } catch (Exception e) { + e.fillInStackTrace(); + } } return null; }