废话少说,先上图,大家加了用了试试。
目前实现微信在线翻译(英-》汉)、快递单、股票信息、天气\PM2.5等功能,当然还有好多彩蛋功能。
下面上主要代码:2个XML,一个是存文字,一个是图片信息。
private final static String menuString="天气:发送地名或者位置。\r\n" + "PM2.5:发送地名或者位置\r\n" + "股票行情:发送股票名称或者是代码 \r\n" + "快递单号:发送快递单号\r\n" + "英文翻译:发送英文单词\r\n"; private static Logger log = Logger.getLogger(ChaXunZhuShouServlet.class .getName()); private static final String RESPONSE_TXT = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>"; private static final String RESPONSE_IMAGE = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType></xml>"; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); String nonce = req.getParameter("nonce"); String echostr = req.getParameter("echostr"); System.out.println("get Method running :" + signature + " : " + timestamp + " : " + nonce + " : " + echostr); PrintWriter out = resp.getWriter(); out.write(echostr); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); String nonce = req.getParameter("nonce"); String echostr = req.getParameter("echostr"); Scanner scanner = new Scanner(req.getInputStream(), "utf-8"); resp.setContentType("application/xml"); resp.setCharacterEncoding("UTF-8"); PrintWriter out = resp.getWriter(); log.info(signature + " : " + timestamp + " : " + nonce + " : " + echostr); try { // 1、获取用户发送的信息 StringBuffer sb = new StringBuffer(100); while (scanner.hasNextLine()) { sb.append(scanner.nextLine()); } System.out.println("获取用户输入的数据!" + sb.toString()); // 2、解析用户的信息 JAXBContext jc = JAXBContext.newInstance(WeChatReqBean.class); Unmarshaller u = jc.createUnmarshaller(); WeChatReqBean reqBean = (WeChatReqBean) u .unmarshal(new StringReader(sb.toString())); WeChatRespBean respBean=null; if (reqBean.getMsgType().equals(WeixinConstants.MSGTYPE_EVENT)) { // 这是新用户关注时默认发的一条信息。可以做一个欢迎处理。 respBean=new WeChatRespBean(); respBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); respBean.setContent("感谢您关注手机查询助手," + menuString); } else if (reqBean.getMsgType().equals(WeixinConstants.MSGTYPE_TEXT)) { String keyword = reqBean.getContent(); respBean=CommonInfoGet.getContent(keyword); }else if(reqBean.getMsgType().equals(WeixinConstants.MSGTYPE_LOCATION)){ respBean=LocaltionInfo.getLocaltionInfo(reqBean); }else { respBean=new WeChatRespBean(); respBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); respBean.setContent("我暂时还不懂您的意思\r\n" + menuString); } String reponseXml=RESPONSE_TXT; String type="text"; if(null!=respBean.getMsgType()){ if( respBean.getMsgType().equals(WeixinConstants.MSGTYPE_IMAGE)){ String xml=XmlUtil.appendXmlInNode(RESPONSE_IMAGE,respBean.getArticles()); reponseXml=xml; type="news"; } }else{ respBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); respBean.setContent("我暂时还不懂您的意思\r\n" + menuString); } out.printf(reponseXml, reqBean.getFromUserName(), reqBean .getToUserName(), System.currentTimeMillis(), type, respBean.getContent()); } catch (Exception e) { e.printStackTrace(); } finally { if (scanner != null) { scanner.close(); scanner = null; } if (out != null) { out.close(); out = null; } } }
核心数据取值类:
扫描二维码关注公众号,回复:
672687 查看本文章
![](/qrcode.jpg)
public class CommonInfoGet { public static WeChatRespBean getContent(String keyword) throws IOException { WeChatRespBean responseBean=new WeChatRespBean(); if(keyword.length()>=FetchExpressInfo.minLength && keyword.length()<=FetchExpressInfo.maxLength){ String expressStr=FetchExpressInfo.getExpressInfo(keyword); if(!"".equals(expressStr)){ responseBean.setContent(expressStr); responseBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); return responseBean; } } if(IsChineseOrEnglish.strIsEnglish(keyword)){ String translate=TranslateInfo.getTranslateInfo(keyword); if(!"".equals(translate)){ responseBean.setContent(translate); responseBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); return responseBean; } } StringBuffer returnStr=new StringBuffer(); StringReader read = new StringReader(keyword); IKSegmenter ik = new IKSegmenter(read, true); Lexeme lex = null; boolean stock=false; boolean area=false; String stockCode=""; SingleInfo info=SingleInfo.getStockSingleInfo(); System.out.println("SingleInfo="+info); Map<String,String> stockMap=info.getStockMap(); Map<String,String> areaMap=info.getAreaMap(); String mapAreaName=""; while((lex=ik.next())!=null){ String str = lex.getLexemeText(); System.out.println(str + "\t"); if(stockMap.containsKey(str)){ stockCode=stockMap.get(str); stock=true; } if(areaMap.containsKey(str)){ area=true; mapAreaName=areaMap.get(str); } } if(stock){ returnStr.append(FetchStockInfo.findStockInfo(stockCode)); returnStr.append("\r\n"); responseBean.setContent(returnStr.toString()); responseBean.setMsgType(WeixinConstants.MSGTYPE_TEXT); } if(area){ WeatherInfo.getWeatherInfo(keyword,mapAreaName,responseBean); PMInfo.getPMInfo(keyword,mapAreaName,responseBean); } return responseBean; } }
欢迎大家关注提意见。谢谢。