xmpp - Android aSmack - How to make custom message extension for file sending? -


i'm trying implement offline file extension chat application. i'm using packetextension , embeddedextensionprovider adding , parsing custom extension. can see values , tags added while sending message, @ receiver end these absent. i'm doing wrong ?

sending message:

<message id="224k2-7" to="testfirzan@sushant" type="chat">     <body>hi</body>     <custom xmlns="jabber2:x2:oob2">       <url>url</url>       <desc>description</desc>     </custom> </message> 

receiving message:

system.out(734): embeddedextensionprovider  <message id="224k2-7" to="testfirzan@sushant" from="testsushant@sushant/smack" type="chat">     <body>hi</body>     <custom xmlns="jabber2:x2:oob2"></custom> </message>  file extension description:(734): <custom xmlns="jabber2:x2:oob2"></custom> 

packet extension , provider manager code:

public class fileextension implements packetextension {  public static final string element = "custom"; public static final string namespace = "jabber2:x2:oob2";  string description; string url;  @override public string getelementname() {     return "custom"; }  @override public string getnamespace() {     return "jabber2:x2:oob2"; }  @override public string toxml() {      stringbuilder builder = new stringbuilder();     builder.append("<" + getelementname() + " xmlns=\"" + getnamespace()             + "\">");     if (url != null) {         builder.append("<url>").append(url).append("</url>");     }     if (description != null) {         builder.append("<desc>").append(description).append("</desc>");     }     builder.append("</" + getelementname() + ">");     return builder.tostring(); }  public string getdescription() {     return description; }  public void setdescription(string description) {     this.description = description; }  public string geturl() {     return url; }  public void seturl(string imageurl) {     this.url = imageurl; }  public static class fileextensionprovider extends embeddedextensionprovider {      @override     protected packetextension createreturnextension(string currentelement,             string currentnamespace, map<string, string> attributemap,             list<? extends packetextension> content) {          system.out.println("embeddedextensionprovider");         return new fileextension();     } } } 

registering provider :

providermanager.getinstance().addextensionprovider(             fileextension.element, fileextension.namespace,             new fileextension.fileextensionprovider()); 

while sending message :

fileextension fileext = new fileextension("url");     fileext.setdescription("description");     fileext.seturl("url");      msg.addextension(fileext); 

after receiving message :

fileextension fileext = (fileextension) message                             .getextension("jabber2:x2:oob2");                      log.i("file extension description:", fileext.toxml()); 

following complete solution making custom extension asmack:

1st had create packetextension:

public class extensionfile implements packetextension {      public static final string element = "file";     public static final string namespace = "file:namespace";      private string fileurl;     private string filesize;      @override     public string getelementname() {         return element;     }      @override     public string getnamespace() {         return namespace;     }      @override     public string toxml() {          stringbuilder builder = new stringbuilder();         builder.append("<" + getelementname() + " xmlns=\"" + getnamespace()             + "\">");         if (fileurl != null) {             builder.append("<fileurl>").append(fileurl).append("</fileurl>");         }         if (filesize != null) {             builder.append("<filesize>").append(filesize).append("</filesize>");         }         builder.append("</" + getelementname() + ">");         return builder.tostring();     }      public void setfileurl(string fileurl) {         this.fileurl = fileurl;     }      public string getfileurl() {         return fileurl;     }      public void setfilesize(string filesize) {         this.filesize = filesize;     }      public string getfilesize() {         return filesize;     } } 

then provider parse packet:

public class extensionproviderfile implements packetextensionprovider {  @override public packetextension parseextension(xmlpullparser parser)         throws exception {      extensionfile fileext = new extensionfile();      int eventtype = parser.next();      while (eventtype == xmlpullparser.start_tag) {          string name = parser.getname();          switch (parser.geteventtype()) {              case xmlpullparser.start_tag:                 if (name.equals("fileurl")) {                      fileext.setfileurl(parser.nexttext());                 } else if (name.equals("filesize")) {                      fileext.setfilesize(parser.nexttext());                 }         }          eventtype = parser.next();     }      return fileext; } } 

sending message file extension:

private void sendmessage(string recipient, string message){      message msg = new message(recipient,message.type.chat);     msg.setbody(message);      //create extension , set values     extensionfile fileext = new extensionfile();     fileext.setfileurl("http://www.example.com/upload_files/"+ abc.jpg);     fileext.setfilesize(filesize);      //add extension message     msg.addextension(fileext);      //send message     xmppconnection.sendpacket(msg); } 

to receive message implement packetlistener , override processpacket():

@override public void processpacket(packet packet) {      message message = (message) packet;      //get extension message     extensionfile extfile = (extensionfile) message                     .getextension("file:namespace");      if (extfile != null) {          //get values extension         string filepath = extfile.getfileurl();         string filesize = extfile.getfilesize();          system.out.println("message :" + message.getbody() + " file url:" + filepath + " file size:" + filesize);     } } 

and add provide before connecting xmpp server:

providermanager.getinstance().addextensionprovider(             extensionfile.element, extensionfile.namespace,             new extensionproviderfile()); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -