Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

android - How to add custom fields in <message> elements of XMPP stanza/packet?

I want to send

<message id="qm5Dx-8"
 to="abc"
 type="chat" 
 from="abc"
 msgType="2"
 msgTimeStamp="1413971599039"
 fileSize="18 MB" 
 fileHeight="300"
 fileWidth="300"
 thumbnail="abc"
 mediaURL=""
 serverMediaURL="xyz"
 isFromMe="1"
 status="1"><body>Image</body><request xmlns='urn:xmpp:receipts'/></message>

The way i am constructing the custom message is :

public class MyCustomMessage  extends Message{

    public MyCustomMessage(){
        super();
    }

    public MyCustomMessage(String to, Type type){
        super(to, type);
    }
    private String msgType ;
    private String msgTimeStamp ;
    private String isFromMe ;
    private String status ;
    private String mediaURL ;
    private String serverMediaURL ;
    private String fileSize ;
    private String fileHeight ;
    private String fileWidth ;
    private String thumbnail ;

    @Override
    public String toXML() {
        String XMLMessage = super.toXML();
        String XMLMessage1 = XMLMessage.substring(0, XMLMessage.indexOf(">"));
        String XMLMessage2 = XMLMessage.substring(XMLMessage.indexOf(">"));

        if (this.msgType != null) {
            XMLMessage1 += " msgType="" + this.msgType + """;
        }
        if (this.msgTimeStamp != null) {
            XMLMessage1 += " msgTimeStamp="" + this.msgTimeStamp + """;
        }
        if (this.fileSize != null) {
            XMLMessage1 += " fileSize="" + this.fileSize + """;
        }
        if (this.fileHeight != null) {
            XMLMessage1 += " fileHeight="" + this.fileHeight + """;
        }
        if (this.fileWidth != null) {
            XMLMessage1 += " fileWidth="" + this.fileWidth + """;
        }
        if (this.thumbnail != null) {
            XMLMessage1 += " thumbnail="" + this.thumbnail + """;
        }
        if (this.mediaURL != null) {
            XMLMessage1 += " mediaURL="" + this.mediaURL + """;
        }
        if (this.serverMediaURL != null) {
            XMLMessage1 += " serverMediaURL="" + this.serverMediaURL + """;
        }
        if (this.isFromMe != null) {
            XMLMessage1 += " isFromMe="" + this.isFromMe + """;
        }
        if (this.status != null) {
            XMLMessage1 += " status="" + this.status + """;
        }

        return XMLMessage1 + XMLMessage2;
    }

// Setters Getters of all these fields..

}

Then after adding required fields in SmackableImplement class, i m calling mXMPPConnection.sendPacket(customMessage);

but m not receiving any packet. my connections is being closed everytime after calling this method. I have gone through many tutorials but couldnt find any solution...tell me where m mistaken.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Smack surely has a better way to work with XML than this approach with modifying the string representation. This will break badly when anything contains a " or any of the other characters that need to be escaped as an attribute.

  2. You have to add custom payloads to messages as a separate XML element within the message, not as attributes on the message. Your XML should look like:

     <message id="qm5Dx-8" to="abc" type="chat" from="abc">
        <body>Image</body>
        <request xmlns='urn:xmpp:receipts'/>
        <data xmlns='http://bstkaal/custom/data'
          msgType="2"
          msgTimeStamp="1413971599039"
          fileSize="18 MB" 
          fileHeight="300"
          fileWidth="300"
          thumbnail="abc"
          mediaURL=""
          serverMediaURL="xyz"
          isFromMe="1"
          status="1" />
    </message>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...