package com.basic.security.utils; import android.os.SystemClock; import android.text.TextUtils; import com.basic.security.activity.MainActivity; import com.basic.security.base.BaseApplication; import com.basic.security.manager.AlarmManager; import com.basic.security.manager.SettingManager; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SocketClient extends Thread { public static boolean needRestart = false; public static long lastUpdateTime = System.currentTimeMillis(); static Lock socketLock = new ReentrantLock(); static List sockets = new ArrayList<>(); static List connectedSockets = new ArrayList<>(); static Lock connectedSocketsLock = new ReentrantLock(); public static Comparator> mapComparator = new Comparator>() { public int compare(Map m1, Map m2) { try { double score1 = Double.parseDouble(m1.get("likePer")); double score2 = Double.parseDouble(m2.get("likePer")); return (int) (score1 - score2); } catch (Exception e) { e.printStackTrace(); } return 0; } }; public Socket socket = null; static List> newRemoteAlarmList = null; byte[] buff = new byte[1 * 1024 * 1024]; private String ip; private int port; public SocketClient(String ip, int port) { this.ip = ip; this.port = port; } public static void closeAllSockets() { socketLock.lock(); for (Socket socket : sockets) { try { if (socket != null) { socket.close(); } } catch (Exception e) { e.printStackTrace(); } } sockets.clear(); socketLock.unlock(); } public static void socketAdd(Socket socket) { socketLock.lock(); sockets.add(socket); socketLock.unlock(); } public native void setup(String ip, int port); public native String receive(); public static void parseJson(String msg) { JsonParser parser = new JsonParser(); boolean isJSON = true; JsonElement element = null; try { element = parser.parse(msg); } catch (JsonParseException e) { System.out.println("exception: " + msg + " " + e); isJSON = false; } newRemoteAlarmList = new ArrayList<>(); if (isJSON && element != null) { Object obj = element.getAsJsonObject(); JsonArray dataList = (JsonArray) ((JsonObject) obj).get("datalist"); for (int i = 0; i < dataList.size(); i++) { JsonObject je = (JsonObject) dataList.get(i); Map newRemoteAlarm = new HashMap<>(); for (Map.Entry entry : je.entrySet()) { Object value = entry.getValue(); String key = entry.getKey(); if (value != null) { if ("baseInfo".equals(key)) { List> matchPersonMapList = new ArrayList<>(); List matchPersonStrList = new ArrayList<>(); String matchPersonListStr = ""; if (value instanceof JsonArray) { JsonArray matchPersonList = (JsonArray) value; for (int ii = 0; ii < matchPersonList.size(); ii++) { JsonObject matchPerson = (JsonObject) matchPersonList.get(ii); Map matchPersonMap = new HashMap<>(); for (Map.Entry matchPersonEntry : matchPerson.entrySet()) { String matchPersonKey = matchPersonEntry.getKey(); Object matchPersonValue = matchPersonEntry.getValue(); if (matchPersonValue != null) { String matchPersonValueStr = matchPersonValue.toString(); if (matchPersonValueStr.startsWith("\"")) { matchPersonValueStr = matchPersonValueStr.substring(1); } if (matchPersonValueStr.endsWith("\"")) { matchPersonValueStr = matchPersonValueStr.substring(0, matchPersonValueStr.length() - 1); } matchPersonMap.put(matchPersonKey, matchPersonValueStr); } else { matchPersonMap.put(matchPersonKey, ""); } } matchPersonMap.put("mathPersonStr", matchPersonMap.get("tableName") + " " + matchPersonMap.get("personName")); matchPersonMapList.add(matchPersonMap); } Collections.sort(matchPersonMapList, mapComparator); for (Map matchPersonMap : matchPersonMapList) { matchPersonStrList.add(matchPersonMap.get("mathPersonStr")); } matchPersonListStr = TextUtils.join("`", matchPersonStrList); } newRemoteAlarm.put("matchPersonListStr", matchPersonListStr); } else { newRemoteAlarm.put(entry.getKey(), value.toString()); } } } newRemoteAlarmList.add(newRemoteAlarm); } // if (newRemoteAlarmList != null && newRemoteAlarmList.size() > 0) { // System.out.println("receive"); // if (1==1) { // return; // } AlarmManager.renameKeys(newRemoteAlarmList); if (AlarmManager.saveRemoteAlarmListToSqlite(newRemoteAlarmList) > 0) { ((MainActivity) BaseApplication.getApplication().activity).fragment_home.refreshGridView(); } } else { if (((MainActivity) BaseApplication.getApplication().activity).currentFragment == ((MainActivity) BaseApplication.getApplication().activity).fragment_home) { ToastUtil.show("获取报警数据失败"); } } } } @Override public void run() { super.run(); BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String msg = null; try { while (true) { if (1 != 1) { System.out.println("SocketClient.run setup 重连"); setup(ip, port); while (true) { try { String json = receive(); System.out.println("json=" + json); lastUpdateTime = System.currentTimeMillis(); if (json.length() > 0) { if (json.equals("-1")) { System.out.println("SocketClient.run setup 错误"); SystemClock.sleep(100); break; } parseJson(json); } else { SystemClock.sleep(100); } } catch (Exception e) { e.printStackTrace(); } } SystemClock.sleep(1000); // continue; } else { try { if (1 != 1) { msg = "{ \t\"datalist\": [ \t\t{ \t\t\t\"content\": \"\", \t\t\t\"id\": \"8a769d1c-886c-487c-a934-e92b09d8f189\", \t\t\t\"personIsHub\": \"1\", \t\t\t\"picAddress\": \"学校南围墙西段中向东 周界\", \t\t\t\"picDate\": \"2019-05-17 12:19:26\", \t\t\t\"picMaxUrl\": \"http://58.118.225.79:44280/group4/M00/07/4E/rBEy8VzbdN2AVqJIABGqRF1VFR0033.jpg\", \t\t\t\"picSmUrl\": \"http://58.118.225.79:44280/group3/M00/05/18/rBEy81yy6n2AFGElABQIY3pylXU412.jpg\", \t\t\t\"sdkType\": \"入侵\", \t\t\t\"taskName\": \"taskName刘欣\", \t\t\t\"videoIp\": \"学校南围墙西段中向东 周界\", \t\t\t\"videoNum\": \"http://58.118.225.79:44480/videosource/210235C23NF187000168/201905/15/2019051510/210235C23NF187000168$2019-05-15-10-09-37_454639_454888.mp4\", \t\t\t\"videoReqNum\": \"cid3\" \t\t}, \t\t{ \t\t\t\"ID\": \"68a6fe5c-1cb1-4e3c-bc4a-22d678f76e8d\", \t\t\t\"IDCard\": [ \t\t\t\t\"54319001230310455431\", \t\t\t\t\"54319001102134569375\", \t\t\t\t\"54319001310104514563\", \t\t\t\t\"54319001100121231977\", \t\t\t\t\"54319001202319872643\", \t\t\t\t\"54319001230310455431\", \t\t\t\t\"54319001102134569375\" \t\t\t], \t\t\t\"ageDescription\": \"青年\", \t\t\t\"baseInfo\": [ \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001230310455431\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"43\", \t\t\t\t\t\"monitorLevel\": \"2\", \t\t\t\t\t\"personId\": \"70171c10-d8ba-4b7c-a7ec-94d4f89a8d7d\", \t\t\t\t\t\"personName\": \"石破天\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfb968957dfa\", \t\t\t\t\t\"phoneNum\": \"11093456217\", \t\t\t\t\t\"tableId\": \"80a5935d-c6b4-4678-9c76-a6b01c07725b\", \t\t\t\t\t\"tableName\": \"底库124\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001102134569375\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"41\", \t\t\t\t\t\"monitorLevel\": \"3\", \t\t\t\t\t\"personId\": \"68d89b3e-e4db-42eb-ac75-aa8c1082a087\", \t\t\t\t\t\"personName\": \"孙悟空\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfbaa2516ad2\", \t\t\t\t\t\"phoneNum\": \"11031248765\", \t\t\t\t\t\"tableId\": \"5a544e15-2c40-4e54-a12b-f6a0f71f3fbd\", \t\t\t\t\t\"tableName\": \"底库123\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001310104514563\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"78\", \t\t\t\t\t\"monitorLevel\": \"1\", \t\t\t\t\t\"personId\": \"83a42f98-ad21-466f-97aa-24bff1b0340f\", \t\t\t\t\t\"personName\": \"黄飞鸿\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfbb19faece3\", \t\t\t\t\t\"phoneNum\": \"11067854653\", \t\t\t\t\t\"tableId\": \"0cb7f275-9fb3-434e-9122-e59f551443ea\", \t\t\t\t\t\"tableName\": \"底库123412\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001100121231977\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"20\", \t\t\t\t\t\"monitorLevel\": \"2\", \t\t\t\t\t\"personId\": \"fed7b71c-d5eb-4feb-86d9-0522d7034412\", \t\t\t\t\t\"personName\": \"李逍遥\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9601/11,06dfb71c698169\", \t\t\t\t\t\"phoneNum\": \"11012344321\", \t\t\t\t\t\"tableId\": \"f008bf89-9406-490d-868b-6dd679cae954\", \t\t\t\t\t\"tableName\": \"同步库2\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001202319872643\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"40\", \t\t\t\t\t\"monitorLevel\": \"3\", \t\t\t\t\t\"personId\": \"f290bb54-4de8-496b-8612-a8c563ab45e1\", \t\t\t\t\t\"personName\": \"令狐冲\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9601/8,06dfb8facdaf8a\", \t\t\t\t\t\"phoneNum\": \"11039876431\", \t\t\t\t\t\"tableId\": \"ce228150-448b-4e70-9b08-374ed7c5bc3e\", \t\t\t\t\t\"tableName\": \"123\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001230310455431\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"86\", \t\t\t\t\t\"monitorLevel\": \"1\", \t\t\t\t\t\"personId\": \"70171c10-d8ba-4b7c-a7ec-94d4f89a8d7d\", \t\t\t\t\t\"personName\": \"石破天\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfb968957dfa\", \t\t\t\t\t\"phoneNum\": \"11093456217\", \t\t\t\t\t\"tableId\": \"af6dd3b3-dc94-42ee-8347-f12214c2562f\", \t\t\t\t\t\"tableName\": \"同步库3\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001102134569375\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"66\", \t\t\t\t\t\"monitorLevel\": \"2\", \t\t\t\t\t\"personId\": \"68d89b3e-e4db-42eb-ac75-aa8c1082a087\", \t\t\t\t\t\"personName\": \"孙悟空\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfbaa2516ad2\", \t\t\t\t\t\"phoneNum\": \"11031248765\", \t\t\t\t\t\"tableId\": \"2e0640d0-33e5-45e6-b024-3fe165c6f9e9\", \t\t\t\t\t\"tableName\": \"测试底库名称超长字数测试\" \t\t\t\t} \t\t\t], \t\t\t\"content\": \"\", \t\t\t\"gender\": \"男\", \t\t\t\"id\": \"68a6fe5c-1cb1-4e3c-bc4a-22d678f76e8d\", \t\t\t\"indeviceName\": \"视频分析设备244\", \t\t\t\"personIsHub\": \"1\", \t\t\t\"picAddress\": \"测试地址\", \t\t\t\"picDate\": \"2019-05-17 12:19:21\", \t\t\t\"picMaxUrl\": \"http://58.118.225.79:44280/group4/M00/07/6A/rBEy8VzbhTaABvYpAAgn7hKYC2g311.jpg\", \t\t\t\"picSmUrl\": \"http://58.118.225.79:44280/group2/M00/00/B4/rBEy9Fx1Bn2ASQ6FAABDPo9CwFw073.jpg\", \t\t\t\"race\": \"黄人\", \t\t\t\"sdkType\": \"人脸\", \t\t\t\"taskName\": \"run\", \t\t\t\"videoIp\": \"172.17.50.244\", \t\t\t\"videoNum\": \"http://192.168.1.203:9600/12,017dae9d21ac08\", \t\t\t\"videoReqNum\": \"cid3\" \t\t}, \t\t{ \t\t\t\"content\": \"\", \t\t\t\"id\": \"fea3e88f-a2c4-4a07-a19d-89c336f446ee\", \t\t\t\"personIsHub\": \"1\", \t\t\t\"picAddress\": \"学校南围墙西段东向西 周界\", \t\t\t\"picDate\": \"2019-05-17 12:19:21\", \t\t\t\"picMaxUrl\": \"http://58.118.225.79:44280/group4/M00/07/4E/rBEy8VzbdN2AVqJIABGqRF1VFR0033.jpg\", \t\t\t\"picSmUrl\": \"http://58.118.225.79:44280/group3/M00/05/18/rBEy81yy6Y-AORXtABZqoERSp9I640.jpg\", \t\t\t\"sdkType\": \"入侵\", \t\t\t\"taskName\": \"taskName刘欣\", \t\t\t\"videoIp\": \"学校南围墙西段东向西 周界\", \t\t\t\"videoNum\": \"http://58.118.225.79:44480/videosource/210235C23NF187000168/201905/15/2019051510/210235C23NF187000168$2019-05-15-10-09-37_454639_454888.mp4\", \t\t\t\"videoReqNum\": \"cid2\" \t\t}, \t\t{ \t\t\t\"content\": \"\", \t\t\t\"id\": \"5fa82d38-b725-45dc-907e-a7ee0ad3e803\", \t\t\t\"personIsHub\": \"1\", \t\t\t\"picAddress\": \"学校南围墙西段东向西 周界\", \t\t\t\"picDate\": \"2019-05-17 12:19:16\", \t\t\t\"picMaxUrl\": \"http://58.118.225.79:44280/group4/M00/07/4E/rBEy8VzbdN2AVqJIABGqRF1VFR0033.jpg\", \t\t\t\"picSmUrl\": \"http://58.118.225.79:44280/group3/M00/05/18/rBEy81yy6Y2AHleCABVr-OZBNps388.jpg\", \t\t\t\"sdkType\": \"入侵\", \t\t\t\"taskName\": \"taskName刘欣\", \t\t\t\"videoIp\": \"学校南围墙西段东向西 周界\", \t\t\t\"videoNum\": \"http://58.118.225.79:44480/videosource/210235C23NF187000168/201905/15/2019051510/210235C23NF187000168$2019-05-15-10-09-37_454639_454888.mp4\", \t\t\t\"videoReqNum\": \"cid1\" \t\t}, \t\t{ \t\t\t\"ID\": \"3bce3eef-2062-456e-b5cb-d1a4cc63c158\", \t\t\t\"IDCard\": [ \t\t\t\t\"54319001202319872643\", \t\t\t\t\"54319001230310455431\", \t\t\t\t\"54319001102134569375\", \t\t\t\t\"54319001310104514563\", \t\t\t\t\"54319001100121231977\", \t\t\t\t\"54319001202319872643\" \t\t\t], \t\t\t\"ageDescription\": \"青年\", \t\t\t\"baseInfo\": [ \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001202319872643\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"3\", \t\t\t\t\t\"monitorLevel\": \"1\", \t\t\t\t\t\"personId\": \"f290bb54-4de8-496b-8612-a8c563ab45e1\", \t\t\t\t\t\"personName\": \"令狐冲\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9601/8,06dfb8facdaf8a\", \t\t\t\t\t\"phoneNum\": \"11039876431\", \t\t\t\t\t\"tableId\": \"2e0640d0-33e5-45e6-b024-3fe165c6f9e9\", \t\t\t\t\t\"tableName\": \"测试底库名称超长字数测试\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001230310455431\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"31\", \t\t\t\t\t\"monitorLevel\": \"2\", \t\t\t\t\t\"personId\": \"70171c10-d8ba-4b7c-a7ec-94d4f89a8d7d\", \t\t\t\t\t\"personName\": \"石破天\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfb968957dfa\", \t\t\t\t\t\"phoneNum\": \"11093456217\", \t\t\t\t\t\"tableId\": \"80a5935d-c6b4-4678-9c76-a6b01c07725b\", \t\t\t\t\t\"tableName\": \"底库124\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001102134569375\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"1\", \t\t\t\t\t\"monitorLevel\": \"3\", \t\t\t\t\t\"personId\": \"68d89b3e-e4db-42eb-ac75-aa8c1082a087\", \t\t\t\t\t\"personName\": \"孙悟空\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfbaa2516ad2\", \t\t\t\t\t\"phoneNum\": \"11031248765\", \t\t\t\t\t\"tableId\": \"5a544e15-2c40-4e54-a12b-f6a0f71f3fbd\", \t\t\t\t\t\"tableName\": \"底库123\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001310104514563\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"1\", \t\t\t\t\t\"monitorLevel\": \"1\", \t\t\t\t\t\"personId\": \"83a42f98-ad21-466f-97aa-24bff1b0340f\", \t\t\t\t\t\"personName\": \"黄飞鸿\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9600/12,06dfbb19faece3\", \t\t\t\t\t\"phoneNum\": \"11067854653\", \t\t\t\t\t\"tableId\": \"0cb7f275-9fb3-434e-9122-e59f551443ea\", \t\t\t\t\t\"tableName\": \"底库123412\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001100121231977\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"男\", \t\t\t\t\t\"likePer\": \"48\", \t\t\t\t\t\"monitorLevel\": \"2\", \t\t\t\t\t\"personId\": \"fed7b71c-d5eb-4feb-86d9-0522d7034412\", \t\t\t\t\t\"personName\": \"李逍遥\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9601/11,06dfb71c698169\", \t\t\t\t\t\"phoneNum\": \"11012344321\", \t\t\t\t\t\"tableId\": \"f008bf89-9406-490d-868b-6dd679cae954\", \t\t\t\t\t\"tableName\": \"同步库2\" \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\"IDCard\": \"54319001202319872643\", \t\t\t\t\t\"content\": \"\", \t\t\t\t\t\"gender\": \"女\", \t\t\t\t\t\"likePer\": \"97\", \t\t\t\t\t\"monitorLevel\": \"3\", \t\t\t\t\t\"personId\": \"f290bb54-4de8-496b-8612-a8c563ab45e1\", \t\t\t\t\t\"personName\": \"令狐冲\", \t\t\t\t\t\"personPicUrl\": \"http://192.168.1.203:9601/8,06dfb8facdaf8a\", \t\t\t\t\t\"phoneNum\": \"11039876431\", \t\t\t\t\t\"tableId\": \"ce228150-448b-4e70-9b08-374ed7c5bc3e\", \t\t\t\t\t\"tableName\": \"123\" \t\t\t\t} \t\t\t], \t\t\t\"content\": \"\", \t\t\t\"gender\": \"男\", \t\t\t\"id\": \"3bce3eef-2062-456e-b5cb-d1a4cc63c158\", \t\t\t\"indeviceName\": \"视频分析设备244\", \t\t\t\"personIsHub\": \"1\", \t\t\t\"picAddress\": \"测试地址\", \t\t\t\"picDate\": \"2019-05-17 12:19:16\", \t\t\t\"picMaxUrl\": \"http://58.118.225.79:44280/group4/M00/07/6A/rBEy8VzbhTaABvYpAAgn7hKYC2g311.jpg\", \t\t\t\"picSmUrl\": \"http://58.118.225.79:44280/group2/M00/00/B4/rBEy9Fx1Bn-AS7idAAAgkMt8drY443.jpg\", \t\t\t\"race\": \"黑人\", \t\t\t\"sdkType\": \"人脸\", \t\t\t\"taskName\": \"face\", \t\t\t\"videoIp\": \"172.17.50.244\", \t\t\t\"videoNum\": \"http://192.168.1.203:9600/12,017dae9d21ac08\", \t\t\t\"videoReqNum\": \"cid2\" \t\t} \t], \t\"total\": 5 }"; parseJson(msg); SystemClock.sleep(2000); continue; } if (System.currentTimeMillis() - lastUpdateTime < 30 * 1000) { SystemClock.sleep(10 * 1000); continue; } ip = SettingManager.getIpStr(); port = SettingManager.getPort(); try { closeAllSockets(); System.out.println("断开原来所有连接"); } catch (Exception e) { e.printStackTrace(); } SystemClock.sleep(10 * 1000); socket = new Socket(); socketAdd(socket); socket.connect(new InetSocketAddress(ip, port), 3000); System.out.println("连接成功"); // socket.connect(new InetSocketAddress(ip, 7000), 3000); inputStream = new BufferedInputStream(socket.getInputStream()); outputStream = new BufferedOutputStream(socket.getOutputStream()); int len = 0; lastUpdateTime = System.currentTimeMillis(); while ((len = inputStream.read(buff)) != -1) { for (int i1 = 0; i1 < len; i1++) { if (buff[i1] == '\0') { try { byte[] msgBuf = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.reset(); msg = new String(msgBuf, 0, msgBuf.length); msg = msg.substring(0, msg.lastIndexOf("}") + 1); System.out.println("msg=" + msg); if (msg.length() > 3) { parseJson(msg); } } catch (Exception e) { e.printStackTrace(); } lastUpdateTime = System.currentTimeMillis(); } else { byteArrayOutputStream.write(buff[i1]); } } if (needRestart) { needRestart = false; break; } } } catch (Exception e) { System.out.println("SocketClient:110, " + e.getMessage()); if (e instanceof InterruptedException) { System.out.println("中断异常, 退出程序"); return; } } finally { if (socket != null) { try { socket.close(); socket = null; } catch (Exception e) { e.printStackTrace(); } } System.out.println("准备20秒后重连"); SystemClock.sleep(20 * 1000); } } } } catch (Exception e) { e.printStackTrace(); if (e instanceof InterruptedException) { System.out.println("中断异常, 退出程序"); return; } } finally { try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void closeSocket() { try { if (socket != null) { socket.close(); } } catch (Exception e) { e.printStackTrace(); } } }