使用Java调用企业微信打卡记录接口,获取打卡数据并同步
/**
* 构建请求打卡记录并请求返回Json
* @param accessToken
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public void GetPunchRecordData(Context ctx,List<String> listCount, String accessToken) {
// TODO Auto-generated method stub
JSONObject PunchRecordJsonData = null;
/**
* @author siyu
* @Desciption 构建参数
* @Param opencheckindatatype 是 打卡类型。1:上下班打卡;2:外出打卡;3:全部打卡
* @Param starttime 是 获取打卡记录的开始时间。Unix时间戳
* @Param endtime 是 获取打卡记录的结束时间。Unix时间戳
* @Param useridlist 是 需要获取打卡记录的用户列表
* @Param 同步周期为4个小时同步一次
*/
RequestParam BuildParam = new RequestParam();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
ca.setTime(new Date()); //设置时间为当前时间
ca.add(Calendar.HOUR, -4);//前4个小时
Date lastMonth = ca.getTime(); //结果
/**
* 转换成UnixTimeStamp
*/
String StartUnixTimeStamp = String.valueOf(sdf.parse(sdf.format(lastMonth)).getTime() / 1000);
String EndUnixTimeStamp = String.valueOf(sdf.parse(sdf.format(new Date())).getTime() / 1000);
System.out.println(sdf.format(lastMonth)+",Unix时间戳为:"+StartUnixTimeStamp);
System.out.println(sdf.format(new Date())+"Unix时间戳为:"+EndUnixTimeStamp);
BuildParam.setType(PunchRecordType);
BuildParam.setStarttime(StartUnixTimeStamp);
BuildParam.setEndTime(EndUnixTimeStamp);
BuildParam.setUseridList(listCount);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
logger.error("构建请求打卡记录并请求返回Json ParseException时间戳转换异常为:"+e1);
}
//构建请求
String strParam = JSONObject.toJSONString(BuildParam);
CloseableHttpClient httpClient=null;
String option = "https";
if(option.equals("https")) {
httpClient =(CloseableHttpClient) wrapClient();
}else {
httpClient = HttpClients.createDefault();
}
String res = "";
StringBuffer PostRequestUrl = new StringBuffer();
PostRequestUrl.append(RequestPunchRecord).append(accessToken);
HttpPost httpPost = new HttpPost(PostRequestUrl.toString());
httpPost.setHeader("content-type", "application/json");
//RequestConfig requestConfig = ().setSocketTimeout(200000).setConnectTimeout(200000).build();
//(requestConfig);
try {
if (null != strParam) {
StringEntity entity;
entity = new StringEntity(strParam, "UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute((HttpUriRequest) httpPost);
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
str = EntityUtils.toString(result.getEntity(), "UTF-8");
res = str;
System.out.println("获取到接口的返回值为:"+res);
PunchRecordJsonData = JSONObject.parseObject(res);
/**
* 调用数据同步
*/
SyncPunchRecordData(ctx, PunchRecordJsonData,accessToken);
}else{
System.out.println("获取打卡数据的时候,接口请求错误!");
return;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}