如何使用SMACK库处理客户端的XMPP XEP-0363:HTTP文件上载功能?

时间:2016-01-26 04:40:37

标签: android xmpp smack

SMACK是否具有处理“XEP-0363:HTTP文件上传”功能的客户端界面? 如果没有,是否有相同的其他标准客户端接口?

我可以使用自定义IQ

来完成
    IQ iq = new IQ("request", "urn:xmpp:http:upload") {
        @Override
        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
            xml.rightAngleBracket();
            xml.element("filename", "text.png");
            xml.element("size", "1645");
            return xml;
        }
    iq.setType(IQ.Type.get);
    iq.setFrom("testa@localhost/Smack");
    iq.setTo("upload.localhost");

connection.sendStanza(iq);

1 个答案:

答案 0 :(得分:0)

我使用简单类来实现此功能:

vertices=(12 14 16)
edgefactors=(16)
printf "mBallStringinc\n" 
for vertex in ${vertices[*]}
do
   for edgefactor in ${edgefactors[*]}
   do
        ./mBallStringinc ${vertex} ${edgefactor}
   done
done

然后,创建实现package mypkg; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.provider.IntrospectionProvider; import org.jivesoftware.smack.provider.ProviderManager; import org.jivesoftware.smack.tcp.XMPPTCPConnection; public class XEP0363 implements StanzaListener{ private XMPPTCPConnection connection; private String id; private SlotGrantedListener listener; static { ProviderManager.addIQProvider("slot", "urn:xmpp:http:upload", new SlotIQProvider()); } public static class SlotIQProvider extends IntrospectionProvider.IQIntrospectionProvider { public SlotIQProvider() { super(SlotIQ.class); } } public static class SlotIQ extends IQ { private String put; private String get; public SlotIQ() { super("request", "urn:xmpp:http:upload"); } public String getPut() { return put; } public void setPut(String put) { this.put = put; } public String getGet() { return get; } public void setGet(String get) { this.get = get; } @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { return null; } } public interface SlotGrantedListener { void slotGranted(String put, String get); void slotDenied(String error); } public XEP0363(XMPPTCPConnection connection) { this.connection = connection; } public void requestSlot(String to, final String fileName, final long fileSize, SlotGrantedListener listener) throws SmackException.NotConnectedException { this.listener = listener; final IQ iq = new IQ("request", "urn:xmpp:http:upload") { @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { xml.rightAngleBracket(); xml.element("filename", fileName); xml.element("size", String.valueOf(fileSize)); return xml; } }; iq.setType(IQ.Type.get); iq.setFrom(connection.getUser()); iq.setTo(to); id = iq.getStanzaId(); connection.addAsyncStanzaListener(this, new StanzaFilter() { @Override public boolean accept(Stanza stanza) { return id != null && id.equals(stanza.getStanzaId()); } }); connection.sendStanza(iq); } @Override public void processPacket(Stanza packet) throws SmackException.NotConnectedException { connection.removeAsyncStanzaListener(this); if (((IQ)packet).getType() != IQ.Type.error) { SlotIQ slotIQ = (SlotIQ) packet; listener.slotGranted(slotIQ.getPut(), slotIQ.getGet()); } else { XMPPError error = packet.getError(); String errDesc = error.getDescriptiveText(null); String errText = errDesc != null ? String.format("%s (%s)", errDesc, error.toString()) : error.toString(); listener.slotDenied(errText); } } } 并调用SlotGrantedListener的侦听器:

requestSlot()

请注意,此类不是线程安全的。