>
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭连接
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
connection.disconnect();
}
return result.toString();
}
/**
* 调用登录接口获取sessionId与sign
* @return
*/
public static Map<String,String> iotLogin() {
HashMap<String, String> paramHashMap = new HashMap<>();
//调用登录接口获取sessionId与sign
//设置登录接口参数
String loginParam=" {\"userCode\": "+userCode+",\"passWord\":"+passWord +"}";
String loginResult =doPost(UrlAddress.IOT_LOGIN, loginParam,paramHashMap);
Map mapType = JSON.parseObject(loginResult,Map.class);
Map result = (Map) mapType.get("result");
if ((int)mapType.get("resultCode")==0){
//将数据存到
HttpURLConnectionUtil.setMapCache(result);
}
return cacheMap;
}
/**
* 调用北向接口方法
* @return
*/
public static Map iotCallMothod(JMap params, String str) throws Exception {
//调用北向服务的接口
//1.在缓存中获取sessionId与sign
Map<String, String> mapCache = HttpURLConnectionUtil.getMapCache();
if(mapCache.isEmpty()||mapCache.get("sign")==null||mapCache.get("sessionId")==null){
//2.如果没有调用登录接口从新获取
HttpURLConnectionUtil.iotLogin();
}
//3.调用北向服务接口
//(1)将参数转为json
String jmapParam= JSON.toJSONString(params);
//(2)调用接口
String resultJson = HttpURLConnectionUtil.doPost(str, jmapParam,mapCache);
//(3)将参数转为json
Map<String,String> resultMap = JSON.parseObject(resultJson, Map.class);
//判断是否调用成功
if("0".equals(resultMap.get("resultCode"))){
//成功返回
return resultMap;
}else{
//4.调用不成功过期,清除缓存,在调用登录接口
HttpURLConnectionUtil.clear();
HttpURLConnectionUtil.iotLogin();
//5.调用北向服务接口
//(2)调用接口
String fianlresultJson = HttpURLConnectionUtil.doPost(str, jmapParam,mapCache);
//(3)将json转为f返回值
Map<String,String> fianlresultMap = JSON.parseObject(fianlresultJson, Map.class);
//返回
return fianlresultMap;
}
}
private static final ILogger logger = IpuLoggerFactory.createLogger(HttpURLConnectionUtil.class);
private static String userCode;
private static String passWord;
@Value("${aap.iot.userCode}")
public void setUserCode(String userCode) {
HttpURLConnectionUtil.userCode = userCode;
}
@Value("${aap.iot.passWord}")
public void setPassWord(String passWord) {
HttpURLConnectionUtil.passWord = passWord;
}
// 定义静态存储map空间存放sign与sessionId
private volatile static Map<String, String> cacheMap = new ConcurrentHashMap<String, String>();// 缓存map
// set
public static void setMapCache(Map<String, String> map) {
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
cacheMap.put(key, map.get(key));
}
}
// get
public static Map<String, String> getMapCache() {
return cacheMap;
}
// 清除cache
public static void clear() {
cacheMap.clear();
}
/**
* 调用登录接口获取sessionId与sign
*
* @return
*/
public static Map<String, String> iotLogin() {
logger.debug("登录北向接口");
// 调用登录接口获取sessionId与sign
HashMap<String, String> loginParamMap = new HashMap<>();
loginParamMap.put("userCode", userCode);
loginParamMap.put("passWord", passWord);
// 设置字符集
Charset charset = Charset.forName("utf-8");
// 调用登录接口
String loginResult = HttpServiceUtil.sendPost(UrlAddress.IOT_LOGIN, loginParamMap, charset);
Map mapType = JSON.parseObject(loginResult, Map.class);
Map result = (Map) mapType.get("result");
if ("0".equals(String.valueOf(mapType.get("resultCode")))) {
logger.info("登录北向接口成功");
// 将数据存到缓存中
HttpURLConnectionUtil.setMapCache(result);
} else {
logger.info("登录北向接口失败");
}
return cacheMap;
}
/**
* 调用北向接口方法
*
* @return
*/
public static Map<String, String> iotCallMothod(String url, Map<String, String> params) throws Exception {
// 调用北向服务的接口
logger.debug("调用北向接口");
// 1.在缓存中获取sessionId与sign
Map<String, String> mapCache = HttpURLConnectionUtil.getMapCache();
if (mapCache.isEmpty() || mapCache.get("sign") == null || mapCache.get("sessionId") == null) {
// 2.如果没有调用登录接口从新获取
HttpURLConnectionUtil.iotLogin();
}
// 3.调用北向服务接口
// 设置字符集
Charset charset = Charset.forName("utf-8");
// (1)调用接口
String resultJson = HttpServiceUtil.sendPost(url, params, charset);
// (2)将参数转为Map<String,String>【将返回值统一为String】
Map<String, String> resultMap = JSON.parseObject(resultJson, Map.class);
// 登录超时,需重新登录
if ("登录超时".equals(resultMap.get("resultMsg"))) {
logger.info("调用北向接口失败,需重新登录");
// 4.调用不成功可能是登录过期
// (1)清除缓存
HttpURLConnectionUtil.clear();
// (2)重新登录
HttpURLConnectionUtil.iotLogin();
// (3)再次调用接口
String fianlresultJson = HttpServiceUtil.sendPost(url, params, charset);
// (4)获取返回值
resultMap = JSON.parseObject(fianlresultJson, Map.class);
}
// 判断是否调用成功
if ("0".equals(resultMap.get("resultCode"))) {
logger.info("调用北向接口成功");
} else {
logger.info("调用北向接口失败");
}
return resultMap;
}
}
|
||
1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|