/*** * 参数签名实例化 * @param method * @param paramsMap * @return */ protected MapparamsInit(String method, Map paramsMap) { Map map = new HashMap (); long time = System.currentTimeMillis() / 1000; StringBuilder paramString = new StringBuilder(); List paramList = new ArrayList (); for (Iterator it = paramsMap.keySet().iterator(); it.hasNext();) { String key1 = it.next(); String param = key1 + ":" + paramsMap.get(key1); paramList.add(param); } String[] params = paramList.toArray(new String[paramList.size()]); Arrays.sort(params); for (String param : params) { paramString.append(param).append(","); } paramString.append("method").append(":").append(method).append(","); paramString.append("time").append(":").append(time).append(","); paramString.append("secret").append(":").append(YingshiUtils.APP_SECRET); String sign = null; try { sign = DigestUtils.md5Hex(paramString.toString().trim().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Map systemMap = new HashMap (); systemMap.put("ver", "1.0"); systemMap.put("sign", sign); systemMap.put("key", YingshiUtils.APP_KEY); systemMap.put("time", time); map.put("system", systemMap); map.put("method", method); map.put("params", paramsMap); map.put("id", "123456"); return map; } /*** * httpclient post 请求 * @param map * @return */ protected String doPost(Map map) { String json = JSON.toJSONString(map); ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory(); Protocol.registerProtocol("https", new Protocol("https", fcty, 443)); HttpClient client = new HttpClient(); // 使用POST方法 PostMethod method = new PostMethod(YingshiUtils.POST_HOST); String result = ""; try { RequestEntity entity = new StringRequestEntity(json, "application/json", "UTF-8"); method.setRequestEntity(entity); client.executeMethod(method); InputStream inputStream = method.getResponseBodyAsStream(); result = IOUtils.toString(inputStream); } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 method.releaseConnection(); } return result; } /** * Title: getAccessToken
*Description: 获取token 默认有效期为7天
* @return */ public MapgetAccessToken() { Map paramsMap = new HashMap (); paramsMap.put("phone", YingshiUtils.PHONE); Map map = paramsInit(YingshiUtils.method_getAccessToken, paramsMap); Map resultMap = resolveTokenJson(doPost(map)); return resultMap; } /** * Title: cameraList
*Description: 获取设备列表
*/ public String cameraList(String token ,int pageSize,int pageStart) { MapparamsMap = new HashMap (); paramsMap.put("accessToken",token); paramsMap.put("pageSize", pageSize); paramsMap.put("pageStart", pageStart); Map map = paramsInit(YingshiUtils.method_cameraList, paramsMap); doPost(map);// String result = doPost(map);// Map resultMap = resolveCameraList(result); return doPost(map); } /*** * 解析token * @param json * @return */ public Map resolveTokenJson(String json){ Map map = new HashMap (); try { JSONObject json1 = new JSONObject(json); JSONObject json2 = json1.optJSONObject("result"); String code = json2.optString("code"); String msg = json2.optString("msg"); if(code.equals("200")){ JSONObject json3 = json2.optJSONObject("data"); String accessToken = json3.optString("accessToken"); String userId = json3.optString("userId"); map.put("accessToken", accessToken); map.put("userId", userId); } } catch (JSONException e) { e.printStackTrace(); } return map; }