使用HttpURLConnection调用webservice接口

package com.wantwant.iwantmobi.bpm.service.impl;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wantwant.iwantmobi.bpm.dao.jdbc.BpmAttendanceDao;
import com.wantwant.iwantmobi.bpm.pojo.AnnualQuota;
import com.wantwant.iwantmobi.bpm.pojo.ExcepMsg;
import com.wantwant.iwantmobi.bpm.pojo.OvertimeOrLeave;
import com.wantwant.iwantmobi.bpm.pojo.Vacation;
import com.wantwant.iwantmobi.bpm.service.IBpmAttendanceService;


import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;


@Service
public class BpmAttendanceService implements IBpmAttendanceService{

private Logger logger = Logger.getLogger(BpmAttendanceService.class);
@Autowired BpmAttendanceDao bpmAttendanceDao;

/*private String getA(String a){
// 1年假固定配额 3事假配额 4病假配额 10三八配额 11年假浮动配额 12五四配额 13慰劳假配额 21年假固定配额(去年度) 31年假浮动配额(去年度)33慰劳假配额(之前剩余)
         
        switch(a){
case "01": return "年假固定配额 ";
case "03": return "年假固定配额 ";
case "04": return "年假固定配额 ";
case "10": return "三八配额";
case "11": return "年假浮动配额";
case "12": return "五四配额 ";
case "13": return "慰劳假配额";
case "21": return "年假固定配额(去年度)";
case "31": return "年假浮动配额(去年度)";
//case "33": return "慰劳假配额(之前剩余)";
default: return "慰劳假配额(之前剩余)";
}
}*/

@Override
public Map<String, Object> getAnnualQuotaList(String userId) {
// TODO Auto-generated method stub
Map<String,Object> rtnMap = new HashMap<>();
List<AnnualQuota> annualQuotaList = new ArrayList<>();
try {
//String annualQuotaUrl="http://nwcep73.want-want.com:50600/QueryNjpeInfoService/QueryNjpeInfoService";
String annualQuotaUrl=ResourceBundle.getBundle("/properties/connection").getString("annualQuotaUrl");
//System.out.println(annualQuotaUrl);
//创建指定链接的url对象
URL url = new URL(annualQuotaUrl);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//设置请求方式
con.setRequestMethod("POST");
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
            con.setInstanceFollowRedirects(true);
//设置第一次请求的数据内容不被存储
//con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
//获取xml数据
//String xmlInfo = getXmlInfo(empId);
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.kqser.want.com/\">"
+ "<soapenv:Header/><soapenv:Body><ser:queryNjpeInfo>"
//+ "<!--Optional:-->"
+ "<empId>"+userId+"</empId>"
+ "</ser:queryNjpeInfo></soapenv:Body></soapenv:Envelope>";
//向指定链接写入数据
out.write(new String(xml.getBytes("UTF-8")));
out.flush();
out.close();
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
/*for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}*/
while ((line = br.readLine()) != null) {
                buf.append(line);
            }


//获取服务端返回的HttpCode
int httpCode = con.getResponseCode();
//System.out.println("HttpCode:"+httpCode+"  "+"接口:"+buf.toString());


XMLSerializer xmlSerializer=new XMLSerializer();
        JSON json=xmlSerializer.read(buf.toString());
        //System.out.println("重点处:\n"+json.toString(1)+"\n");
        //System.out.println(json.toString());
        
        //截取掉[],转化为JSONObject           
        //JSONObject jsonObject=JSONObject.fromObject((json.toString()).substring(json.toString().lastIndexOf('@')-2, json.toString().lastIndexOf(']')+2));
        //System.out.println("截取后:\n"+jsonObject.toString()+"\n");
        
        JSONObject json2 = JSONObject.parseObject(json.toString());         
        //System.out.println(json2.get("SOAP-ENV:Body"));
        
        JSONObject json3 = JSONObject.parseObject(json2.get("SOAP-ENV:Body").toString());         
        //System.out.println(json3.get("ns2:queryNjpeInfoResponse"));
        
        JSONObject json4 = JSONObject.parseObject(json3.get("ns2:queryNjpeInfoResponse").toString());         
        //System.out.println(json4.get("return").toString());
        logger.info("------来自NetWeaver的年度配额信息-----:"+json4.get("return").toString());
        //第一种方式  
        @SuppressWarnings("unchecked")
List<Map<String,Object>> listObjectFir = (List<Map<String,Object>>) JSONArray.parse(json4.get("return").toString());  
        //System.out.println("利用JSONArray中的parse方法来解析json数组字符串");  
        
        for(Map<String,Object> mapList : listObjectFir){  
       
            //System.out.println(mapList);
            String a=(String) mapList.get("KTART");
            if (a.equals("01")) {
a="年假固定配额";
}
            if (a.equals("03")) {
a="事假配额";
}
            if (a.equals("04")) {
a="病假配额";
}
            if (a.equals("10")) {
a="三八配额";
}
            if (a.equals("11")) {
a="年假浮动配额";
}
            if (a.equals("12")) {
a="五四配额";
}
            if (a.equals("13")) {
a="慰劳假配额";
}
            if (a.equals("21")) {
a="年假固定配额(去年度)";
}
            if (a.equals("31")) {
a="年假浮动配额(去年度)";
}
            if (a.equals("33")) {
a="慰劳假配额(之前剩余)";
}
            
            //this.getA(a);
            
            String b=(String) mapList.get("KVERB");
            String c=(String) mapList.get("QUOTAOFFSET");
            String d=(String) mapList.get("ANZHL");
            String e=(String) mapList.get("QUOTALAST");
            String f=(String) mapList.get("QUOTAPER");
            //System.out.println("假別:"+a+"  已使用配额:"+b+"  补偿配额:"+c+"  年度配额:"+d+"  剩余配额:"+e+"  配额有效期间:"+f);
                                
            AnnualQuota annualQuota=new AnnualQuota();
            annualQuota.setVacationTitle(a);
            annualQuota.setUsedQuota(b);
            annualQuota.setCompensationQuota(c);
            annualQuota.setAnnualQuota(d);
            annualQuota.setResidualQuota(e);
            annualQuota.setQuotaValidPeriod(f);
            
            annualQuotaList.add(annualQuota);
        }    
        logger.info("-------------向前端展示的年度配额信息------------"+annualQuotaList);
        rtnMap.put("annualQuotaList", annualQuotaList);
                
} catch (MalformedURLException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return rtnMap;

}


@Override
public Map<String, Object> getExcepMsgList(String userId) {
// TODO Auto-generated method stub
Map<String,Object> rtnMap = new HashMap<>();
List<ExcepMsg> excepMsgList = new ArrayList<>();
try {
//String excepMsgUrl="http://nwcep73.want-want.com:50600/QueryKQycInfoService/QueryKQycInfoService";
String excepMsgUrl=ResourceBundle.getBundle("/properties/connection").getString("excepMsgUrl");
//System.out.println(excepMsgUrl);
//创建指定链接的url对象
URL url = new URL(excepMsgUrl);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//设置请求方式
con.setRequestMethod("POST");
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
            con.setInstanceFollowRedirects(true);
//设置第一次请求的数据内容不被存储
//con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
//获取xml数据
//String xmlInfo = getXmlInfo(empId);
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.kqser.want.com/\">"
+ "<soapenv:Header/><soapenv:Body><ser:queryKQException>"
//+ "<!--Optional:-->"
+ "<empId>"+userId+"</empId>"
+ "</ser:queryKQException></soapenv:Body></soapenv:Envelope>";
//向指定链接写入数据
out.write(new String(xml.getBytes("UTF-8")));
out.flush();
out.close();
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
/*for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}*/
while ((line = br.readLine()) != null) {
                buf.append(line);
            }


//获取服务端返回的HttpCode
int httpCode = con.getResponseCode();
//System.out.println("HttpCode:"+httpCode+"  "+"接口:"+buf.toString());
//logger.info("---------------buf.toString()------------"+buf.toString());

XMLSerializer xmlSerializer=new XMLSerializer();
        JSON json=xmlSerializer.read(buf.toString());
        //System.out.println("重点处:\n"+json.toString(1)+"\n");
        //System.out.println(json.toString());
        
        //截取掉[],转化为JSONObject           
        //JSONObject jsonObject=JSONObject.fromObject((json.toString()).substring(json.toString().lastIndexOf('@')-2, json.toString().lastIndexOf(']')+2));
        //System.out.println("截取后:\n"+jsonObject.toString()+"\n");
        
        JSONObject json2 = JSONObject.parseObject(json.toString());         
        //System.out.println(json2.get("SOAP-ENV:Body"));
        
        JSONObject json3 = JSONObject.parseObject(json2.get("SOAP-ENV:Body").toString());         
        //System.out.println(json3.get("ns2:queryKQExceptionResponse"));
        
        JSONObject json4 = JSONObject.parseObject(json3.get("ns2:queryKQExceptionResponse").toString());         
        //System.out.println(json4.get("return"));
        logger.info("-------------来自NetWeaver的考勤异常信息------------"+json4.get("return"));
        
        //第一种方式  
        @SuppressWarnings("unchecked")
List<Map<String,Object>> listObjectFir = (List<Map<String,Object>>) JSONArray.parse(json4.get("return").toString());  
        //System.out.println("利用JSONArray中的parse方法来解析json数组字符串");  
        
        for(Map<String,Object> mapList : listObjectFir){  
       
            //System.out.println(mapList);
        //logger.info("-----------mapList--------------"+mapList);
       
            String a=(String) mapList.get("date");
            String b=mapList.get("exceptionMsg").toString();
            //System.out.println("日期:"+a+"  异常信息:"+b);
            
            if (!"[]".equals(b)) {
            ExcepMsg excepMsg=new ExcepMsg(); 
        excepMsg.setAbnormalDate(a);
        excepMsg.setAbnormalMsg(b);
        
        excepMsgList.add(excepMsg);
}                         
        }    
        logger.info("-------------向前端展示的考勤异常信息------------"+excepMsgList);
        rtnMap.put("excepMsgList", excepMsgList);
                
} catch (MalformedURLException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return rtnMap;
}


@Override
public Map<String, Object> getOvertimeOrLeaveList(String atype,String awart, String begda, 
String beguz,String docid,String doctype,String endda, String enduz, String userId) {
// TODO Auto-generated method stub
Map<String,Object> rtnMap = new HashMap<>();
try {
//String overtimeOrLeaveUrl="http://nwcep73.want-want.com:50600/QueryJbssService/QueryJbssService";
String overtimeOrLeaveUrl=ResourceBundle.getBundle("/properties/connection").getString("overtimeOrLeaveUrl");
//创建指定链接的url对象
URL url = new URL(overtimeOrLeaveUrl);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//设置请求方式
con.setRequestMethod("POST");
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
            con.setInstanceFollowRedirects(true);
//设置第一次请求的数据内容不被存储
//con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
//获取xml数据
//String xmlInfo = getXmlInfo(empId);

String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.kqser.want.com/\">"
+ "<soapenv:Header/><soapenv:Body><ser:QueryJbssServ>"
//+ "<!--Optional:--> "
+ "<arg0>"
+ "<ATYPE>"+atype+"</ATYPE>"  //类型 :0 1 2 3 4 5 6
+ "<AWART>"+awart+"</AWART>"  //awart 出缺勤类型
+ "<BEGDA>"+begda+"</BEGDA>"  //begda 开始日期
+ "<BEGUZ>"+beguz+"</BEGUZ>" //beguz 开始时间
+ "<DOCID>"+docid+"</DOCID>"    //BPM单号
+ "<DOCTYPE>"+doctype+"</DOCTYPE>"  //doctype 单据类别: 1 表示请假    3 表示加班
+ "<ENDDA>"+endda+"</ENDDA>" //endda 结束日期
+ "<ENDUZ>"+enduz+"</ENDUZ>"  //enduz 结束时间
+ "<PERNR>"+userId+"</PERNR>"  //pernr 员工编号
+ "<SEQNO>"+"?"+"</SEQNO>"
+ "</arg0>"
+ "</ser:QueryJbssServ></soapenv:Body></soapenv:Envelope>";
//向指定链接写入数据
out.write(new String(xml.getBytes("UTF-8")));
out.flush();
out.close();
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
/*for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}*/
while ((line = br.readLine()) != null) {
                buf.append(line);
            }


//获取服务端返回的HttpCode
int httpCode = con.getResponseCode();
//System.out.println("HttpCode:"+httpCode+"  "+"接口:"+buf.toString());

XMLSerializer xmlSerializer=new XMLSerializer();
        JSON json=xmlSerializer.read(buf.toString());
        //System.out.println("重点处:\n"+json.toString(1)+"\n");
        //System.out.println(json.toString());
        
        //截取掉[],转化为JSONObject           
        //JSONObject jsonObject=JSONObject.fromObject((json.toString()).substring(json.toString().lastIndexOf('@')-2, json.toString().lastIndexOf(']')+2));
        //System.out.println("截取后:\n"+jsonObject.toString()+"\n");
        
        JSONObject json2 = JSONObject.parseObject(json.toString());         
        //System.out.println(json2.get("SOAP-ENV:Body"));
        
        JSONObject json3 = JSONObject.parseObject(json2.get("SOAP-ENV:Body").toString());         
        //System.out.println(json3.get("ns2:QueryJbssServResponse"));
        
        JSONObject json4 = JSONObject.parseObject(json3.get("ns2:QueryJbssServResponse").toString());         
        System.out.println(json4.get("return"));
        //第一种方式   
Map<String, Object> ObjectFir = (Map<String, Object>) JSONArray.parse(json4.get("return").toString());
// System.out.println("利用JSONArray中的parse方法来解析json数组字符串");


System.out.println(ObjectFir);


Object a = ObjectFir.get("ABSENCEDAYS");  //请假天数
Object b = ObjectFir.get("ABSENCEHOURS");  //请假时数
Object c = ObjectFir.get("CURWORKSCHEDULE");  //实际班别
Object d = ObjectFir.get("OVERTIMEHOURS");  //加班时数
Object e = ObjectFir.get("RETURNMESSAGE");  //传回讯息内文
Object f = ObjectFir.get("RETURNNUMBER");  //传回讯息号码
Object g = ObjectFir.get("RETURNTYPE");  //传回讯息类型       S:成功,E:错误 , W:警告,I:信息,A:取消



OvertimeOrLeave overtimeOrLeave = new OvertimeOrLeave();
overtimeOrLeave.setAbsencedays(a);
overtimeOrLeave.setAbsencehours(b);
overtimeOrLeave.setCurworkschedule(c);
overtimeOrLeave.setOvertimehours(d);
overtimeOrLeave.setReturnmessage(e);
overtimeOrLeave.setReturnnumber(f);
overtimeOrLeave.setReturntype(g);
logger.info("-------------向前端展示的考勤加班或请假时数------------overtimeOrLeave: "+overtimeOrLeave);
rtnMap.put("overtimeOrLeave", overtimeOrLeave);
                
} catch (MalformedURLException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return rtnMap;
}

/* 
* 判斷用戶身分是本籍或外籍與所擁有的假別配額
* */
@Override
public Map<String, Object> getVacationList(String userId) {
// TODO Auto-generated method stub
Map<String,Object> rtnMap = new HashMap<>();
List<Vacation> vacationList = new ArrayList<>();
List<Vacation> vacationList2 = new ArrayList<>();
String identity=null;
String empClassId = null;
String empAreaId=null;
        try {
empClassId = bpmAttendanceDao.getEmpClassId(userId);
        //empClass是A,H,J的是陆籍,是E,K,F,L,T是台外籍 (hrorg.emp表)        
        if (empClassId.equals("A")||empClassId.equals("H")||empClassId.equals("J")) {
        identity="nationality";
        empClassId="L"; //L是陆籍 (IWANTMOBI.KQ_LEAVER_TYPE 表)
} else {
identity="foreign";
empClassId="T"; //T是台外籍(IWANTMOBI.KQ_LEAVER_TYPE 表)
}
        logger.info("-------------[判斷用戶身分是本籍或外籍與所擁有的假別配額].本籍或外籍------------identity: "+identity);
        empAreaId= bpmAttendanceDao.getEmpAreaId(userId);
        
        vacationList=bpmAttendanceDao.getVacation(empClassId);
        vacationList2=bpmAttendanceDao.getVacation2(empClassId,empAreaId);         
        vacationList.addAll(vacationList2);
        logger.info("-------------[判斷用戶身分是本籍或外籍與所擁有的假別配額].假別配額------------vacationList: "+vacationList);
        if (!"WWZB".equals(empAreaId) ) {
        empAreaId="notWWZB";
}
        logger.info("-------------[判斷用戶身分是本籍或外籍與所擁有的假別配額].是否为“非总部”员工------------empAreaId: "+empAreaId);
        rtnMap.put("vacationList", vacationList);         
        rtnMap.put("identity", identity);
        rtnMap.put("empAreaId", empAreaId);
        
} catch (Exception e) {
logger.error(e.toString());
e.printStackTrace();
}
       
return rtnMap;
}

@Override
public Map<String, Object> getApplicationForm(String userId, String kqType, String endTime,
String startTime,String absencehours,String vacationLocation,String vacationValue,String holidayTypeName,
String reason,String clockInTime,String clockOuTtime,String notClockDate,String workingHoursType,
String offHoursType,String atype,String docid,String workDate,String overtimehours) {
// TODO Auto-generated method stub
Map<String,Object> rtnMap = new HashMap<>();

logger.info("--------加班参数---------"+"userId: "+userId+", kqType: "+kqType+", overtimehours: "+overtimehours+", reason: "+reason+", startTime: "+startTime+", endTime: "+endTime);
logger.info("--------请假参数---------"+"userId: "+userId+", kqType: "+kqType+", vacationValue: "+vacationValue+", holidayTypeName: "+holidayTypeName+
", vacationLocation: "+vacationLocation+", startTime: "+startTime+"endTime: "+endTime+"absencehours: "+absencehours+", reason: "+reason+", atype: "+atype+", docid: "+docid);
try {
//String applicationFormUrl="http://10.0.1.123:50600/StartHrbdkqServiceService/StartHrbdkqService";
String applicationFormUrl=ResourceBundle.getBundle("/properties/connection").getString("applicationFormUrl");
//System.out.println(applicationFormUrl);
//创建指定链接的url对象
URL url = new URL(applicationFormUrl);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//设置请求方式
con.setRequestMethod("POST");
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
con.setDoInput(true);
// post请求缓存设为false 
//con.setUseCaches(false);
  // 设置该HttpURLConnection实例是否自动执行重定向 
            //con.setInstanceFollowRedirects(true);
//设置第一次请求的数据内容不被存储
//con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
//con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
//con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");  
con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "utf-8");
//OutputStream out = con.getOutputStream();



/*
//获取加班小时数
String overtimeHours=null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
if (startTime!=null && endTime!=null) {
try {
Date d2 = df.parse(startTime);
Date d1 = df.parse(endTime);
long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
long days = diff / (1000 * 60 * 60 * 24);
// double hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
int hours = (int) (diff / (1000 * 60 * 60));
double minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
double minutes2 = minutes / 60;
String minutes3=null;
if (minutes2==0.0) {
minutes3="";
}
overtimeHours = hours + minutes3 + "";


} catch (Exception e) {
e.printStackTrace();
}
}
*/

logger.info("-------------1------------");
//UDKtype 未打卡类别:上班时间,workingHoursType 下班时间  offHoursType
String UDKtype=null;
if ("01".equals(workingHoursType) && !"02".equals(offHoursType)) {
UDKtype="上班时间";
clockOuTtime="17:30";
}
if (!"01".equals(workingHoursType) && "02".equals(offHoursType)) {
UDKtype="下班时间";
clockInTime="08:30";
}
if ("01".equals(workingHoursType) && "02".equals(offHoursType)) {
UDKtype="上班时间,下班时间";
}
if (vacationLocation==null) {
vacationLocation="";
}
if (atype==null) {
atype="";
}
if (docid==null) {
docid="";
}
logger.info("-------------2------------");
//获取xml数据
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.hrbdkq.want.com/\">"
+ "<soapenv:Header/><soapenv:Body><ser:startHrbdkqBpm>"
// <!--Optional:-->
+ "<arg0>"
// <!--Optional:-->
//+ "<empCategory>"+empCategory+"</empCategory>" //内勤或者业务(总部内勤-1;非总部业务-2;非总部内勤-3)
// <!--Optional:-->
+ "<empId>"+userId+"</empId>" //申请人ID
// <!--Optional:-->
+ "<kqType>"+kqType+"</kqType>"; //申请类别:加班(jb),请假(qj),未打卡(udk)
// <!--Zero or more repetitions:-->

if("jb".equals(kqType)){
xml+="<jbList>"
// <!--Optional:-->
+ "<appEmpId>"+userId+"</appEmpId>" //申请人id
// <!--Optional:-->
+ "<content>"+reason+"</content>" //加班理由
// <!--Optional:-->
+ "<endDate>"+endTime+"</endDate>" //结束加班时间 yyyy-MM-dd HH:mm
// <!--Optional:-->
+ "<overTimeCount>"+overtimehours+"</overTimeCount>" //加班时数
// <!--Optional:-->
+ "<startDate>"+startTime+"</startDate>"  //起始加班时间 yyyy-MM-dd HH:mm
+ "</jbList>";
}else if("qj".equals(kqType)){
xml+="<qjList>"
+ "<appEmpid>"+userId+"</appEmpid>" //申请人id
+ "<holidayType>"+vacationValue+"</holidayType>" // 申请假别 
+ "<holidayTypeName>"+holidayTypeName+"</holidayTypeName>"  //申请假别名称
+ "<holidayArea>"+vacationLocation+"</holidayArea>" //休假地点
+ "<startDate>"+startTime+"</startDate>"  //起始时间 yyyy-MM-dd HH:mm
+ "<endDate>"+endTime+"</endDate>" //结束时间 yyyy-MM-dd HH:mm
+ "<hourCount>"+absencehours+"</hourCount>" //请假时数
+ "<reason>"+reason+"</reason>" //请假理由
+ "<brDate>"+atype+"</brDate>"  //哺乳假类型
+ " <funeralHolidayCode>"+docid+"</funeralHolidayCode>"  //丧假申请单号(路程假时要填写的参数)
+ "</qjList>";
}else{
xml+="<udkform>"
// <!--Optional:-->
+ "<actualCheckInTime>"+clockInTime+"</actualCheckInTime>" //刷进时间 HH:mm
// <!--Optional:-->
+ "<actualCheckoutTime>"+clockOuTtime+"</actualCheckoutTime>" //刷出时间 HH:mm
// <!--Optional:-->
+ "<appEmpid>"+userId+"</appEmpid>" //申请人id
// <!--Optional:-->
+ "<UDKtype>"+UDKtype+"</UDKtype>"  //未打卡类别:上班时间, 下班时间      workingHoursType   offHoursType
// <!--Optional:-->
+ "<unCheckInDate>"+notClockDate+"</unCheckInDate>" //未打卡日期  yyyy-MM-dd 
// <!--Optional:-->
+ "<unCheckInReason>"+reason+"</unCheckInReason>" //未打卡原因
+ "<workDate>"+workDate+"</workDate>"  //需上班日期
+ "</udkform>";
}

xml+="</arg0>"
+ "</ser:startHrbdkqBpm></soapenv:Body></soapenv:Envelope>";

//向指定链接写入数据
out.write(xml);
logger.info("-------------3 xml------------: "+xml);
out.flush();
out.close();

logger.info("-------------4------------");
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
logger.info("-------------br------------: "+br);
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
/*for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}*/
while ((line = br.readLine()) != null) {
                buf.append(line);
            }
logger.info("-------------5------------");
//获取服务端返回的HttpCode
int httpCode = con.getResponseCode();
//System.out.println("HttpCode:"+httpCode+"  "+"接口:"+buf.toString());

XMLSerializer xmlSerializer=new XMLSerializer();
        JSON json=xmlSerializer.read(buf.toString());
        //System.out.println("重点处:\n"+json.toString(1)+"\n");
        //System.out.println(json.toString());
        logger.info("-------------6------------");
        //截取掉[],转化为JSONObject           
        //JSONObject jsonObject=JSONObject.fromObject((json.toString()).substring(json.toString().lastIndexOf('@')-2, json.toString().lastIndexOf(']')+2));
        //System.out.println("截取后:\n"+jsonObject.toString()+"\n");
        
        JSONObject json2 = JSONObject.parseObject(json.toString());         
        //System.out.println(json2.get("SOAP-ENV:Body"));
        
        JSONObject json3 = JSONObject.parseObject(json2.get("SOAP-ENV:Body").toString());
        //System.out.println(json3.get("ns2:startHrbdkqBpmResponse"));
        
        JSONObject json4 = JSONObject.parseObject(json3.get("ns2:startHrbdkqBpmResponse").toString());         
        //System.out.println(json4.get("return"));
        //第一种方式   
@SuppressWarnings("unchecked")
Map<String, Object> ObjectFir = (Map<String, Object>) JSONArray.parse(json4.get("return").toString());
// System.out.println("利用JSONArray中的parse方法来解析json数组字符串");


System.out.println(ObjectFir);
logger.info("-------------向前端展示的申请单起单------------:"+ObjectFir);
rtnMap.put("applicationForm", ObjectFir);
                
} catch (MalformedURLException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return rtnMap;
}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值