获取接口数据-HttpClient

1.调用接口方法,使用HttpClient,请求报文为xml格式(返回数据 xml,中间使用标签包含json数据)

//传送报文和路径进行接口调用
	public String send(String url, String soapXmlData) {
		String responseBody = "";
        PostMethod post = new PostMethod(url);
        System.out.println("接口发送内容为:" + soapXmlData);
        int state;
		try {
			RequestEntity requestEntity = new StringRequestEntity(soapXmlData,"text/xml","UTF-8");
			post.setRequestEntity(requestEntity);
			HttpClient httpClient = new HttpClient();
			state = httpClient.executeMethod(post);
			responseBody = new String(post.getResponseBodyAsString());
			System.out.println("调用状态是:" + state);
			System.out.println("返回消息是:" + responseBody);
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException(e);
		}
		return responseBody;
	}

//对返回数据解析成map(返回xml的json数据,解析)
//返回map类型的json数据,但需要进行解析xml标签获取json数据
	public Map sendXmlData(String url, String soapXmlData){
		Map respDataMap = new HashMap();
		String respdata = "";
		
		try {
			respdata = send(url, soapXmlData.toString());
			respDataMap = XMLUtil.doXMLParse(respdata);
		} catch (Exception e){
			e.printStackTrace();
		}
		return respDataMap;
	}


2.使用xml报文,将报文中参数设置成参数,传map进行进行对应

/**
	 * 根据模板字符串生成内容
	 * @param strTemplate 模板信息
	 * @param map 数据信息
	 * @return
	 */
	public static String publishHtml(String strTemplate, Map<String, Object> map) {
		//Open Declaration freemarker.template.Configuration
		Configuration cfg = new Configuration();
		String content = null;
		StringWriter stringWriter = new StringWriter(); // 将数据模型和模板合并
		//获取编码格式
		String encoding ="gbk";
		try {
			cfg.setTemplateLoader(new StringTemplateLoader(strTemplate));  
			cfg.setDefaultEncoding(encoding);
			cfg.setOutputEncoding(encoding);
			cfg.setLocale(Locale.CHINA);
			cfg.setNumberFormat("0.####");
			cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
			Template template = cfg.getTemplate("");
			template.setEncoding(encoding);
			template.process(map, stringWriter);
			content = stringWriter.toString();
			stringWriter.flush();
		} catch (IOException e) {
			throw new MisException(e);
		} catch (TemplateException e) {
			throw new MisException(e);
		}  finally {
			if (stringWriter != null) {
				try {
					stringWriter.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		return content;
	}

–报文


<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webService.udsp.pactera.com/">

<soapenv:Header>

<userInfo>

<userId>${USERID}</userId>

<userPwd>${USERPWD}</userPwd>

</userInfo>

</soapenv:Header>

<soapenv:Body>

<web:CommExecute>

<web:arg0>${CODE}</web:arg0>

<web:arg1>


<web:entry>

<web:key>flowId</web:key>

<web:value>${FLOWID}</web:value>

</web:entry>


<web:entry>

<web:key>timeLimit</web:key>

<web:value>${TIMELIMIT}</web:value>

</web:entry>


<web:entry>

<web:key>userIp</web:key>

<web:value>${USERIP}</web:value>

</web:entry>


<web:entry>

<web:key>sysId</web:key>

<web:value>${SYSID}</web:value>

</web:entry>


<web:entry>

<web:key>partId</web:key>

<web:value>${PARTID}</web:value>

</web:entry>


<web:entry>

<web:key>queryType</web:key>

<web:value>${TYPE}</web:value>

</web:entry>


<web:entry>

<web:key>name</web:key>

<web:value>${NAME}</web:value>

</web:entry>


<web:entry>

<web:key>number</web:key>

<web:value>${NUMBER}</web:value>

</web:entry>


<web:entry>

<web:key>dataType</web:key>

<web:value>${DATATYPE}</web:value>

</web:entry>


<web:entry>

<web:key>entryId</web:key>

<web:value>${ENTRYID}</web:value>

</web:entry>


<web:entry>

<web:key>reqType</web:key>

<web:value>${REQTYPE}</web:value>

</web:entry>



</web:arg1>

</web:CommExecute>

</soapenv:Body>

</soapenv:Envelope>



–参数map

		//获取当前服务器ip
		InetAddress ia=InetAddress.getLocalHost();
		String localip = ia.getHostAddress();
								
		Map<String, Object> gysInfo = new HashMap<String, Object>();
		gysInfo.put("NAME", jc_gysxx.getGsmc());
		gysInfo.put("USERID", Constants_tyc.DSJ_FH_USERID);
		gysInfo.put("USERPWD", Constants_tyc.DSJ_FH_USERPWD);
		gysInfo.put("CODE", Constants_tyc.DSJ_FH_GJZ_CODE);
		gysInfo.put("RANGE", Constants_tyc.DSJ_FH_RANGE);
		gysInfo.put("USERIP", localip);//当前服务器ip
		gysInfo.put("SYSID", "CPM");//系统编号
		gysInfo.put("PARTID", "000000");//部门编号
		gysInfo.put("PAGENO", Constants_tyc.DSJ_FH_RANGNO);
		gysInfo.put("TIMELIMIT", Constants_tyc.DSJ_FH_TIMELIMIT_GJZ);

3.返回xml数据需要转换实体类方法(返回数据中间还是xml格式,需要xml转换成实体类)

/**
     * xml转对象
     * 
     * @param xml
     * @param clazz
     * @return
     */
    public static Object xml2java(String xml, Class clazz) {
    	try {
    		JAXBContext content = JAXBContext.newInstance(clazz);
    		Unmarshaller m = content.createUnmarshaller();
    		StringReader sr = new StringReader(xml);
    		Object t = m.unmarshal(sr);
    		return t;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值