Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android XMPP通訊自定義Packet&Provider

Android XMPP通訊自定義Packet&Provider

編輯:關於Android編程

摘要

在xmpp通信過程中,asmack中提供的Packet組件是IQ,Message,Presence三種: IQ用於查詢 Message用於消息傳遞 Presence用於狀態交互 他們都是Packet的子類,實質是用於將消息封裝成響應的xml格式來進行數據交換,都有著良好的可擴展性。

簡介

我們以開源項目androidpn為例:

androidpn (Android Push Notification)是一個基於XMPP協議的java開源Android push notification實現。它包含了完整的客戶端和服務器端。

androidpn包括Server端和Client端,項目名稱是androidpn-server和androidpn-client。

事實上,androidpn-server可以支持app運行於iOS,UWP,Windows,Linux等平台,不僅限於android,因此,希望項目的名稱改為XPN(Xmpp Push Notification)似乎更加符合其實際場景,我們以後涉及到Android Push Notification統稱為XPN。

XNP目前狀態

項目自2014年1月就已經停止更新了,此外,asmack項目也停止更新了,作者建議使用openfire官方的smack4.0,不過這樣的話引入的jar會特別多,特別大。當然,我們下載到了asmack8.10.0比較新的穩定版本,可以完全用於學習和擴展。

項目相關下載站點

asmack-github.com - asmack項目地址

asmack-asmack.freakempire.de - asmack鏡像地址

androidpn(XPN)-github.com - androidpn下載地址

一.關於Packet數據包

Packet是IQ,Message,Presence的父類,用於實現消息組件類型。

消息語義學message

message是一種基本推送消息方法,它不要求響應。主要用於IM、groupChat、alert和notification之類的應用中。
主要 屬性如下:

    type屬性,它主要有5種類型:
        normal:類似於email,主要特點是不要求響應;
        chat:類似於qq裡的好友即時聊天,主要特點是實時通訊;
        groupchat:類似於聊天室裡的群聊;
        headline:用於發送alert和notification;
        error:如果發送message出錯,發現錯誤的實體會用這個類別來通知發送者出錯了;

to屬性:標識消息的接收方。

from屬性:指發送方的名字或標示。為防止地址外洩,這個地址通常由發送者的server填寫,而不是發送者。
載荷(payload):例如body,subject

<message to="[email protected]/contact" 
 type="chat" > 
  <body> 你好,在忙嗎</body> 
</message>

出席信息語義學presence

presence用來表明用戶的狀態,如:online、away、dnd(請勿打擾)等。當改變自己的狀態時,就會在stream的上下文中插入一個Presence元素,來表明自身的狀態。要想接受presence消息,必須經過一個叫做presence subscription的授權過程。
屬性:

type屬性,非必須。有以下類別
    subscribe:訂閱其他用戶的狀態
    probe:請求獲取其他用戶的狀態
    unavailable:不可用,離線(offline)狀態

to屬性:標識消息的接收方。

from屬性:指發送方的名字或標示。

載荷(payload):
    show:
    chat:聊天中
    away:暫時離開
    xa:eXtend Away,長時間離開
    dnd:勿打擾
status:格式自由,可閱讀的文本。也叫做rich presence或者extended presence,常用來表示用戶當前心情,活動,聽的歌曲,看的視頻,所在的聊天室,訪問的網頁,玩的游戲等等。
priority:范圍-128~127。高優先級的resource能接受發送到bare JID的消息,低優先級的resource不能。優先級為
<presence from="[email protected]/pda">
  <show>xa</show>
  <status>down the rabbit hole!</status>
</presence>

IQ語義學

一種請求/響應機制,從一個實體從發送請求,另外一個實體接受請求,並進行響應。例如,client在stream的上下文中插入一個元素,向Server請求得到自己的好友列表,Server返回一個,裡面是請求的結果。
主要的屬性是type。包括:
    Get :獲取當前域值。類似於http get方法。
    Set :設置或替換get查詢的值。類似於http put方法。
    Result :說明成功的響應了先前的查詢。類似於http狀態碼200。
    Error: 查詢和響應中出現的錯誤。
<iq from="[email protected]/pda" 
    id="rr82a1z7"
    to="[email protected]
    type="get">
  <query xmlns="jabber:iq:roster"/>
</iq>

二.自定義Packet

由於服務器和客戶端使用的Packet不同,但是他們交互的數據格式都是xml,因此,這個過程我們理解xml實現過程即可。

1.定義Packet封裝對象

由於asmack標簽解析的限制,我們不能自定義解析,除非修改源碼,這裡出於簡單,這裡只能繼承現有標簽之一。

我麼按照項目代碼NotificationIQ為例,這裡沒有繼承Packet,而是繼承了IQ

import org.jivesoftware.smack.packet.IQ;
/** 
 * This class represents a notifcatin IQ packet.
 *
 * @author Sehwan Noh ([email protected])
 */
public class NotificationIQ extends IQ {
  private String id;
  private String apiKey;
  private String title;
  private String message;
  private String uri;
  public NotificationIQ() {
  }
  @Override
  public String getChildElementXML() {
    StringBuilder buf = new StringBuilder();
    buf.append("<").append("notification").append(" xmlns=\"").append(
        "androidpn:iq:notification").append("\">");
    if (id != null) {
      buf.append("<id>").append(id).append("</id>");
    }
    buf.append("</").append("notification").append("> ");
    return buf.toString();
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getApiKey() {
    return apiKey;
  }
  public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  public String getUri() {
    return uri;
  }
  public void setUri(String url) {
    this.uri = url;
  }
}

其中,getChildElementXml()是IQ的子類,用來拼接成<iq>下的直接點。

public abstract class IQ extends Packet {
  private Type type = Type.GET;
  public IQ() {
    super();
  }
  public IQ(IQ iq) {
    super(iq);
    type = iq.getType();
  }
  /**
   * Returns the type of the IQ packet.
   *
   * @return the type of the IQ packet.
   */
  public Type getType() {
    return type;
  }
  /**
   * Sets the type of the IQ packet.
   *
   * @param type the type of the IQ packet.
   */
  public void setType(Type type) {
    if (type == null) {
      this.type = Type.GET;
    }
    else {
      this.type = type;
    }
  }
  public String toXML() {
    StringBuilder buf = new StringBuilder();
    buf.append("<iq ");
    if (getPacketID() != null) {
      buf.append("id=\"" + getPacketID() + "\" ");
    }
    if (getTo() != null) {
      buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
    }
    if (getFrom() != null) {
      buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
    }
    if (type == null) {
      buf.append("type=\"get\">");
    }
    else {
      buf.append("type=\"").append(getType()).append("\">");
    }
    // Add the query section if there is one.
    String queryXML = getChildElementXML();
    if (queryXML != null) {
      buf.append(queryXML);
    }
    // Add the error sub-packet, if there is one.
    XMPPError error = getError();
    if (error != null) {
      buf.append(error.toXML());
    }
    buf.append("</iq>");
    return buf.toString();
  }
  /**
   * Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
   * isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
   *
   * Extensions of this class must override this method.
   *
   * @return the child element section of the IQ XML.
   */
  public abstract String getChildElementXML();
  /**
   * Convenience method to create a new empty {@link Type#RESULT IQ.Type.RESULT}
   * IQ based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
   * IQ. The new packet will be initialized with:<ul>
   *   <li>The sender set to the recipient of the originating IQ.
   *   <li>The recipient set to the sender of the originating IQ.
   *   <li>The type set to {@link Type#RESULT IQ.Type.RESULT}.
   *   <li>The id set to the id of the originating IQ.
   *   <li>No child element of the IQ element.
   * </ul>
   *
   * @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
   * @throws IllegalArgumentException if the IQ packet does not have a type of
   *   {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
   * @return a new {@link Type#RESULT IQ.Type.RESULT} IQ based on the originating IQ.
   */
  public static IQ createResultIQ(final IQ request) {
    if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
      throw new IllegalArgumentException(
          "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
    }
    final IQ result = new IQ() {
      public String getChildElementXML() {
        return null;
      }
    };
    result.setType(Type.RESULT);
    result.setPacketID(request.getPacketID());
    result.setFrom(request.getTo());
    result.setTo(request.getFrom());
    return result;
  }
  /**
   * Convenience method to create a new {@link Type#ERROR IQ.Type.ERROR} IQ
   * based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
   * IQ. The new packet will be initialized with:<ul>
   *   <li>The sender set to the recipient of the originating IQ.
   *   <li>The recipient set to the sender of the originating IQ.
   *   <li>The type set to {@link Type#ERROR IQ.Type.ERROR}.
   *   <li>The id set to the id of the originating IQ.
   *   <li>The child element contained in the associated originating IQ.
   *   <li>The provided {@link XMPPError XMPPError}.
   * </ul>
   *
   * @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
   * @param error the error to associate with the created IQ packet.
   * @throws IllegalArgumentException if the IQ packet does not have a type of
   *   {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
   * @return a new {@link Type#ERROR IQ.Type.ERROR} IQ based on the originating IQ.
   */
  public static IQ createErrorResponse(final IQ request, final XMPPError error) {
    if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
      throw new IllegalArgumentException(
          "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
    }
    final IQ result = new IQ() {
      public String getChildElementXML() {
        return request.getChildElementXML();
      }
    };
    result.setType(Type.ERROR);
    result.setPacketID(request.getPacketID());
    result.setFrom(request.getTo());
    result.setTo(request.getFrom());
    result.setError(error);
    return result;
  }
  /**
   * A class to represent the type of the IQ packet. The types are:
   *
   * <ul>
   *   <li>IQ.Type.GET
   *   <li>IQ.Type.SET
   *   <li>IQ.Type.RESULT
   *   <li>IQ.Type.ERROR
   * </ul>
   */
  public static class Type {
    public static final Type GET = new Type("get");
    public static final Type SET = new Type("set");
    public static final Type RESULT = new Type("result");
    public static final Type ERROR = new Type("error");
    /**
     * Converts a String into the corresponding types. Valid String values
     * that can be converted to types are: "get", "set", "result", and "error".
     *
     * @param type the String value to covert.
     * @return the corresponding Type.
     */
    public static Type fromString(String type) {
      if (type == null) {
        return null;
      }
      type = type.toLowerCase();
      if (GET.toString().equals(type)) {
        return GET;
      }
      else if (SET.toString().equals(type)) {
        return SET;
      }
      else if (ERROR.toString().equals(type)) {
        return ERROR;
      }
      else if (RESULT.toString().equals(type)) {
        return RESULT;
      }
      else {
        return null;
      }
    }
    private String value;
    private Type(String value) {
      this.value = value;
    }
    public String toString() {
      return value;
    }
  }
}

最終可生成如下結構的數據

<iq from="">
 <nofitication xlns="">
<iq>

我們在項目中的使用很簡單

xmppManager.getConnection().sendPacket(<NotificationIQ>niq)

當然,上面只是實現了object->xml,接下來我們實現xml->data

2.實現IQProvider

先來看看IQProvider源碼

public interface IQProvider {

  /**
   * Parse the IQ sub-document and create an IQ instance. Each IQ must have a
   * single child element. At the beginning of the method call, the xml parser
   * will be positioned at the opening tag of the IQ child element. At the end
   * of the method call, the parser <b>must</b> be positioned on the closing tag
   * of the child element.
   *
   * @param parser an XML parser.
   * @return a new IQ instance.
   * @throws Exception if an error occurs parsing the XML.
   */
  public IQ parseIQ(XmlPullParser parser) throws Exception;
}

實現自定義的解析工具

public class NotificationIQProvider implements IQProvider {
  public NotificationIQProvider() {
  }
  @Override
  public IQ parseIQ(XmlPullParser parser) throws Exception {
    NotificationIQ notification = new NotificationIQ();
    for (boolean done = false; !done;) {
      int eventType = parser.next();
      if (eventType == 2) {
        if ("id".equals(parser.getName())) {
          notification.setId(parser.nextText());
        }
        if ("apiKey".equals(parser.getName())) {
          notification.setApiKey(parser.nextText());
        }
        if ("title".equals(parser.getName())) {
          notification.setTitle(parser.nextText());
        }
        if ("message".equals(parser.getName())) {
          notification.setMessage(parser.nextText());
        }
        if ("uri".equals(parser.getName())) {
          notification.setUri(parser.nextText());
        }
      } else if (eventType == 3
          && "notification".equals(parser.getName())) {
        done = true;
      }
    }
    return notification;
  }
}

項目中使用方法

ProviderManager.getInstance().addIQProvider("notification",
              "androidpn:iq:notification",
              new NotificationIQProvider());

在asmack中PacketParserUtils類中會進行如下調用

 Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
          if (provider != null) {
            if (provider instanceof IQProvider) {
              iqPacket = ((IQProvider)provider).parseIQ(parser);
            }
            else if (provider instanceof Class) {
              iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
                  (Class<?>)provider, parser);
            }
          }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved