首页 > 代码库 > 基于smack的xmpp packet 重写

基于smack的xmpp packet 重写

    基于Smack 实现Notification数据包。smack的类中有一个org.jivesoftware.smack.packet.IQ只需对他重写即可,在做的时候其实可以简单一点的,如果你使用tinder.jar 包,其IQ org.xmpp.packet.IQ 有一个 setChildElement 方法与 getChildElement相对应,但smack没有!

    主要用于与android Client之间Push通信。定义一套自己的数据包格式:

1   <iq id="11111" to="aa@qq.com" type="get">2   <notification xmlns="androidpn:iq:notification">3   <id>123456</id> 4   <apiKey>1234567890</apiKey> 5   <title>nothing</title> 6   <message>jintiantianqibuycuo</message> 7   <uri>heoo</uri> 8   </notification>9   </iq> 

重新写了一下body的部分,代码如下:

  1 /**  2  * Notification 重写XMPP 的IQ packet (Smack)   3  *   4  * @author Charles  5  * @date 2011/10/16  6  *  7  *  8  */  9  10  11 package com.gareatech.testxmpp; 12  13 import org.jivesoftware.smack.packet.IQ; 14  15  16 /** 17  * 重构之后的数据样式 18  * -  19  * <iq id="11111" to="aa@qq.com" type="get">  20  * <notification xmlns="androidpn:iq:notification"> 21  * <id>123456</id>  22  * <apiKey>1234567890</apiKey>  23  * <title>nothing</title>  24  * <message>jintiantianqibuycuo</message>  25  * <uri>heoo</uri>  26  * </notification> 27  * </iq> 28  *  29  * */ 30 public class Notification extends IQ {     31     private Notify notify; 32      33     public Notify getNotify() { 34         return notify; 35     } 36  37     public void setNotify(Notify notify) { 38         this.notify = notify; 39     } 40  41     @Override 42     public String getChildElementXML() { 43         StringBuilder buf = new StringBuilder(); 44         if (notify != null) { 45             buf.append(notify.toXML()); 46         } 47         return buf.toString(); 48     } 49      50     /** 51      * Notify 52      * Body 部分,重写为<> 53      *  54      * */ 55     public static class Notify { 56         private String id; 57         private String apiKey ; 58         private String title; 59         private String message; 60         private String uri; 61          62         public String getId() { 63             return id; 64         } 65  66         public void setId(String id) { 67             this.id = id; 68         } 69  70         public String getApiKey() { 71             return apiKey; 72         } 73  74         public void setApiKey(String apiKey) { 75             this.apiKey = apiKey; 76         } 77  78         public String getTitle() { 79             return title; 80         } 81  82         public void setTitle(String title) { 83             this.title = title; 84         } 85  86         public String getMessage() { 87             return message; 88         } 89  90         public void setMessage(String message) { 91             this.message = message; 92         } 93  94         public String getUri() { 95             return uri; 96         } 97  98         public void setUri(String uri) { 99             this.uri = uri;100         }101 102         public String toXML() {103             StringBuilder buf = new StringBuilder();104             buf.append("<notification xmlns=\"").append("androidpn:iq:notification\">");105             buf.append("<id>").append(id).append("</id>");106             buf.append("<apiKey>").append(apiKey).append("</apiKey>");107             buf.append("<title>").append(title).append("</title>");108             buf.append("<message>").append(message).append("</message>");109             buf.append("<uri>").append(uri).append("</uri>");110             buf.append("</notification>");111             return buf.toString();112         }113     }114 }

 

基于smack的xmpp packet 重写