调用微信接口--机器人自动回复(涉及到远程调用接口以及处理xml文件的方法)图灵机器人

首先 创建一个实体类,具体参数如下:
 
  
package com.wechat.entity;
/**
 * 微信端数据对应的实体类对象
 * @author Administrator
 *
 */
 


public class ReceiveXmlEntity {
	
	private String ToUserName;
	private String FromUserName;
	private String CreateTime;
	private String MsgType;
	private String MsgId;
	private String Content;
	public String getToUserName() {
		return ToUserName;
	}
	public void setToUserName(String toUserName) {
		ToUserName = toUserName;
	}
	public String getFromUserName() {
		return FromUserName;
	}
	public void setFromUserName(String fromUserName) {
		FromUserName = fromUserName;
	}
	public String getCreateTime() {
		return CreateTime;
	}
	public void setCreateTime(String createTime) {
		CreateTime = createTime;
	}
	public String getMsgType() {
		return MsgType;
	}
	public void setMsgType(String msgType) {
		MsgType = msgType;
	}
	public String getMsgId() {
		return MsgId;
	}
	public void setMsgId(String msgId) {
		MsgId = msgId;
	}
	public String getContent() {
		return Content;
	}
	public void setContent(String content) {
		Content = content;
	}
	
	

}
建立一个servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//设置接口的数据格式和编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		//读取微信客户端post发送过来的数据
		InputStream is = request.getInputStream();
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		
		String str = "";
		StringBuffer sb = new StringBuffer();
		while(null != (str = br.readLine())){
			sb.append(str);
		}
		String content = sb.toString();
		
		//调用微信处理流程
		
		String result= WechatController.wechatProcess(content);
		//返回处理完成的结果
		ServletOutputStream os = response.getOutputStream();
		os.write(result.getBytes("UTF-8"));
		os.flush();
		os.close();
		
		
	}

 
  
get请求类参数是url,也是接口的地址
/**
 * http get 请求类
 * @author Administrator
 *
 */
public class HttpGetRequest {
	
	public static String get(String url){
		String result = "";
		try {
			HttpGet request = new HttpGet(url);
			HttpResponse response = HttpClients.createDefault().execute(request);
			result = "";
			if(response.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}

 
  
请求的结果转成json格式
public static String getTulingRe(String info)
	{
		String url = "";
		String tlResult = HttpGetRequest.get(url);
		JSONObject json = JSONObject.fromObject(tlResult);
		tlResult = json.getString("text");
		return tlResult;
	}

 
  
封装返回给微信的信息,是一个xml文件
/**
 * 封装返回给微信端的xml数据
 * @author Administrator
 *
 */
public class FormatXmlResult {
/**
 * 封装xml结果数据
 * @param xml 接收到的xml对象
 * @param tlResult 图灵机器人处理结果
 * @return
 */
	public static String getXmlResult(ReceiveXmlEntity xml,String tlResult){
		StringBuffer sb = new StringBuffer();
		sb.append("");
		return sb.toString();
		
	}
}

 
  
解析xml数据,利用反射等
/**
 * 解析微信端xml数据,转换为对象
 * @author Administrator
 *
 */

public class ParseReceiveXml {
/**
 * 解析微信xml消息
 * @param content 微信端的xml数据
 * @return 转换之后的对象
 */
	public static ReceiveXmlEntity getMsgEntity(String content){
		ReceiveXmlEntity msg = null;
		
		try {
			//将字符串转换为xml对象
			Document doc = DocumentHelper.parseText(content);
			//获取文档的节点
			Element root = doc.getRootElement();
			
			//遍历根节点下所有的子节点
			Iterator iter = root.elementIterator();
			
			//利用反射机制,调用对象的set方法
			Class c = Class.forName("com.wechat.entity.ReceiveXmlEntity");
			//创建实体类
			msg = (ReceiveXmlEntity) c.newInstance();
			
			while(iter.hasNext()){
				Element ele = (Element) iter.next();
				//获取set方法中的参数字段
				Field filed = c.getDeclaredField(ele.getName());
				//获取set方法
				Method method = c.getDeclaredMethod("set"+ele.getName(), filed.getType());
				//调用set方法
				method.invoke(msg, ele.getText());
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}
	
}

 
  
流程控制类
/**
 * 微信流程控制类
 * @author Administrator
 *
 */
public class WechatController {
/**
 * 微信处理流程控制
 * @param content 客户端发送过来的xml数据
 * @return 处理完成,封装好xml结果数据
 */
	public static String wechatProcess(String content){
		
		//解析接收到的xml数据,转为对象
		ReceiveXmlEntity xml = ParseReceiveXml.getMsgEntity(content);
		
		//调用图灵机器人接口处理模块,获取图灵机器人的结果
		String tlResult = TulingController.getTulingRe(xml.getContent());
		
		//封装xml接口的返回数据
		String xmlResult = FormatXmlResult.getXmlResult(xml, tlResult);
		
		return xmlResult;
	}
}

 
  
需要用到的jar包:httpcomponents-core-4.4.1-bin和httpcomponents-client-4.5-bin

你可能感兴趣的:(java)