|
1.發(fā)送,接受消息的常量
- public class MessageConstant {
-
- /**文本 */
- public static final String REQ_MESSAGE_TYPE_TEXT = "text";
- /**圖片*/
- public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
- /**語音*/
- public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
- /**視頻*/
- public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
- /**小視頻*/
- public static final String REQ_MESSAGE_TYPE_SHORTVIDEO = "shortvideo";
- /**地理位置*/
- public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
- /**鏈接消息*/
- public static final String REQ_MESSAGE_TYPE_LINK = "link";
-
-
- /** 事件類型:subscribe(訂閱)*/
- public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
- /**事件類型:unsubscribe(取消訂閱)*/
- public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
- /**二維碼掃描*/
- public static final String EVENT_TYPE_SCAN = "SCAN";
- /**位置類型*/
- public static final String EVENT_TYPE_LOCATION = "LOCATION";
- /**事件類型:CLICK(自定義菜單點(diǎn)擊事件)*/
- public static final String EVENT_TYPE_CLICK = "CLICK";
-
- /**文本 */
- public static final String RES_MESSAGE_TYPE_TEXT = "text";
- /**圖片*/
- public static final String RES_MESSAGE_TYPE_IMAGE = "image";
- /**語音*/
- public static final String RES_MESSAGE_TYPE_VOICE = "voice";
- /**視頻*/
- public static final String RES_MESSAGE_TYPE_VIDEO = "video";
- /**音樂*/
- public static final String RES_MESSAGE_TYPE_MUSIC = "music";
- /**圖文*/
- public static final String RES_MESSAGE_TYPE_NEWS = "news";
-
- }
messageUtil:處理json
- public class MessageUtil {
- /**
- * 解析微信發(fā)來的請(qǐng)求(xml)
- * @author wuyw
- * 2015-7-29下午6:11:41
- * @return
- */
- public static Map<String,String> parseXml(HttpServletRequest request){
- Map<String,String> map = new HashMap<String,String>();
- try {
- //獲取微信傳來的參數(shù)
- InputStream inputStream = request.getInputStream();
- //讀取輸入流
- SAXReader reader = new SAXReader();
- Document document = reader.read(inputStream);
- //獲取xml根元素
- Element root = document.getRootElement();
- @SuppressWarnings("unchecked")
- List<Element> elements = root.elements();
- //遍歷獲取的元素
- for (Element element : elements) {
- map.put(element.getName(), element.getText());
- }
- inputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return map;
- }
-
- /**
- * 擴(kuò)展xstream令其支持CDATA
- */
- public static XStream xStream=new XStream(new XppDriver(){
- public HierarchicalStreamWriter createWriter(Writer out){
- return new PrettyPrintWriter(out){
- //對(duì)所有xml增加CDATA標(biāo)記
- boolean cdata=true;
-
- @SuppressWarnings("rawtypes")
- public void startNode(String name, Class clazz){
- super.startNode(name, clazz);
- }
-
- protected void writeText(QuickWriter writer, String text){
- if(cdata){
- writer.write("<![CDATA[");
- writer.write(text);
- writer.write("]]>");
- }else{
- writer.write(text);
- }
- }
- };
- }
- });
- //文本
- public static String messageToXml(BaseResMsg resMsg){
- xStream.alias("xml", resMsg.getClass());
- return xStream.toXML(resMsg);
- }
-
- }
|