"data":{
"length":3,
"rawData":"base64编码"
}
实现简单的转发,不包括时间
用的是逐条转发,接下来要实现打包成这样一条发送,那样子一条一条发可能会比较慢
{"ts":1451649600512, "values":{"key1":"value1", "key2":"value2"}}
解析一下:
{
“ts”:123456,
"values":{
"key1":"value1",
"key2":"value2"
}
}
用Hashmap
第一种doPostJson返回HttpResponse
public HttpResponse doPostJson(String url, Map<String, String> headerMap,
String content) {
HttpPost request = new HttpPost(url);
addRequestHeader(request, headerMap);
request.setEntity(new StringEntity(content,
ContentType.APPLICATION_JSON));
return executeHttpRequest(request);
}
主函数
HttpResponse responsePostAsynCmd = httpsUtil.doPostJson(urlPostAsynCmd, header, jsonRequest);
String responseBody = httpsUtil.getHttpResponseBody(responsePostAsynCmd);
System.out.println("PostAsynCommand, response content:");
System.out.print(responsePostAsynCmd.getStatusLine());
System.out.println(responseBody);
System.out.println();
第二种doPostJsonGetStatusLine返回StreamClosedHttpResponse
public StreamClosedHttpResponse doPostJsonGetStatusLine(String url, String content) {
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(content,
ContentType.APPLICATION_JSON));
HttpResponse response = executeHttpRequest(request);
if (null == response) {
System.out.println("The response body is null.");
}
return (StreamClosedHttpResponse) response;
}
StreamClosedHttpResponse value = httpsUtil.doPostJsonGetStatusLine(urlTelemetry,content);
System.out.println("QueryDeviceData, response content:"+value);
return httpsUtil.doPostJsonGetStatusLine(urlTelemetry,content);
unix时间戳的转换
/**
* Java将Unix时间戳转换成指定格式日期字符串
* @param timestampString 时间戳 如:"1473048265";
* @param formats 要格式化的格式 默认:"yyyy-MM-dd HH:mm:ss";
*
* @return 返回结果 如:"2016-09-05 16:06:42";
*/
public static String TimeStamp2Date(String timestampString, String formats) {
if (TextUtils.isEmpty(formats))//如果formats为空则默认使用yyyy。。。
formats = "yyyy-MM-dd HH:mm:ss";
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
return date;
}
将Java指定格式日期转换成Unix时间戳
/**
* 日期格式字符串转换成时间戳
*
* @param dateStr 字符串日期
* @param format 如:yyyy-MM-dd HH:mm:ss
*
* @return
*/
public static String Date2TimeStamp(String dateStr, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
取得当前时间戳(精确到秒)
/**
* 取得当前时间戳(精确到秒)
*
* @return nowTimeStamp
*/
public static String getNowTimeStamp() {
long time = System.currentTimeMillis();
String nowTimeStamp = String.valueOf(time/1000);
return nowTimeStamp;
}
搞定!
剩下的问题就是key怎么和数值对应,还有公式不知道