From aeca1003b8abc4b7e5dfe56228827bbcaae937be Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@iotlink.com> Date: 星期四, 18 六月 2020 22:56:43 +0800 Subject: [PATCH] fix pic and video url with new formater --- app/src/main/java/com/basic/security/utils/VideoPath.java | 107 ++++++++++++++++++++--------------------------------- 1 files changed, 40 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/basic/security/utils/VideoPath.java b/app/src/main/java/com/basic/security/utils/VideoPath.java index 437ed92..2ac45d5 100644 --- a/app/src/main/java/com/basic/security/utils/VideoPath.java +++ b/app/src/main/java/com/basic/security/utils/VideoPath.java @@ -1,7 +1,13 @@ package com.basic.security.utils; +import com.basic.security.manager.SettingManager; import com.basic.security.model.Alarm; import com.google.gson.Gson; +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 org.apache.http.util.TextUtils; @@ -14,83 +20,50 @@ import java.util.Map; public class VideoPath { - static Map<String, String> deviceMap = new HashMap<>(); - static Map<String, String> ngxMap = new HashMap<>(); - static Gson gson = new Gson(); - static Gson gson1 = new Gson(); - - static { - { - deviceMap.put("DSVAD010120181119", "http://172.17.50.241:11111/getRecordVideoPath"); - deviceMap.put("DSVAD010220181119", "http://172.17.50.242:11111/getRecordVideoPath"); - deviceMap.put("DSVAD010320181119", "http://172.17.50.243:11111/getRecordVideoPath"); - deviceMap.put("DSVAD010420181119", "http://172.17.50.244:11111/getRecordVideoPath"); - - ngxMap.put("DSVAD010120181119", "http://172.17.50.241/videosource"); - ngxMap.put("DSVAD010220181119", "http://172.17.50.242/videosource"); - ngxMap.put("DSVAD010320181119", "http://172.17.50.243/videosource"); - ngxMap.put("DSVAD010420181119", "http://172.17.50.244/videosource"); - } - - { - -// deviceMap.put("DSVAD010220181119", "http://58.118.225.79:51111/getRecordVideoPath"); -// -// ngxMap.put("DSVAD010120181119","http://58.118.225.79:44180/videosource"); -// ngxMap.put("DSVAD010220181119", "http://58.118.225.79:44280/videosource"); -// ngxMap.put("DSVAD010320181119", "http://58.118.225.79:44380/videosource"); -// ngxMap.put("DSVAD010420181119", "http://58.118.225.79:44480/videosource"); - } - - - } - public static String getVideoUrl(Map<String, String> alarm) { String videoUrl = ""; + String Server = SettingManager.getIpStr(); + String Port = "6000"; + String base_url = "http://" + Server + ":" + Port + "/basic/api/androidAlarm/es/"; + try { - String indeviceId = alarm.get(Alarm.indeviceid);//"DSVAD010120181119"; - String url = deviceMap.get(indeviceId); - if (url == null) { - return videoUrl; - } + String alarmId = alarm.get(Alarm.id); - URL url1 = new URL(url); - HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); + URL url = new URL(base_url+alarmId); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); - conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod("POST"); + conn.setRequestMethod("GET"); + conn.connect(); + int responseCode = conn.getResponseCode(); + if(responseCode == HttpURLConnection.HTTP_OK){ + //寰楀埌鍝嶅簲娴� + InputStream in = new BufferedInputStream(conn.getInputStream()); + String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); - Map<String, String> map1 = new HashMap<>(); - map1.put("imgKey", alarm.get(Alarm.imgKey)); - map1.put("picDate", alarm.get(Alarm.picDate)); - map1.put("videoNum", alarm.get(Alarm.videoReqNum)); - System.out.println("VideoPath.getVideoUrl " + map1); - - String json = gson1.toJson(map1); - - OutputStream os = conn.getOutputStream(); - os.write(json.getBytes("UTF-8")); - os.close(); - - - InputStream in = new BufferedInputStream(conn.getInputStream()); - String out = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); - - Map map = gson.fromJson(out, Map.class); - String filePath = (String) map.get("file_path"); - if (filePath != null && filePath.contains("/cut")) { - String ngxUrl = ngxMap.get(indeviceId); - if (!TextUtils.isEmpty(ngxUrl)) { - videoUrl = ngxUrl + filePath.split("/cut")[1]; +// System.out.println("videoUrl response :" + response); + boolean isJSON = true; + JsonParser parser = new JsonParser(); + JsonElement element = null; + try { + element = parser.parse(response); + } catch (JsonParseException e) { + System.out.println("videoUrl exception: " + response + " " + e); + isJSON = false; } + + if (isJSON && element != null) { + Object obj = element.getAsJsonObject().getAsJsonObject("data").get("videoUrl"); + String result = obj.toString(); + if (result.length() > 0) { + videoUrl = result; + } + } + in.close(); + conn.disconnect(); } - in.close(); - conn.disconnect(); } catch (Exception e) { - System.out.println("VideoPath.getVideoUrl " + e.getMessage()); + System.out.println("videoUrl VideoPath.getVideoUrl " + e.getMessage()); } return videoUrl; } -- Gitblit v1.8.0