1

所以我最近几天尝试了很多......

我为我的电脑构建了一个 XMPP 服务器和一个客户端,没有任何问题。

比我开始为 Android 创建一个客户端......是的......我只是说我只是不想阅读更多错误......

我阅读了有关 XMPP、Android 和 PubSub 错误的所有内容。现在我建立了这个apk。

我能够连接并保持在线。我的问题是无法构建节点。

所以这是我的代码:

`

package com.Eis.androidclient;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.PublishItem;
import org.jivesoftware.smackx.pubsub.SimplePayload;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Client extends Activity {

static {
    XMPPConnection.DEBUG_ENABLED = true;
}

ConnectionConfiguration connConfig = new ConnectionConfiguration(
        "192.168.0.100", 5222);
XMPPConnection connection = new XMPPConnection(connConfig);
String pubSubAddress = "pubsub."+ connection.getServiceName();
PubSubManager manager = new PubSubManager(connection,pubSubAddress);
SimplePayload payload = new SimplePayload("session",
        "pubsub:NewNode:session", "<sessionId>5678</sessionId>");
PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>(null,
        payload);



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    org.jivesoftware.smack.SmackAndroid.init(this);
    Button node = (Button)findViewById(R.id.button1);

    node.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            node();     
        }
    });





    Thread thread = new Thread(){
        public void run(){

            try {
                connection.connect();
                connection.login("tester", "tester");
            } catch (XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };

    thread.run();
    // putting null for id means you let server generate id
    // you could use publish() for asynchronous call


}

public void node(){


    PubSubManager manager = new PubSubManager(connection);
    LeafNode myNode = null;

   // manager.discoverNodes("NewNode");
    try {
        manager.getNode("NewNode");
        System.out.println("found");
        manager.deleteNode("NewNode");
        System.out.println("delete");
    } catch(XMPPException e){
        System.out.println("nothing to do");
        e.printStackTrace();
    }
    try{
        myNode = manager.createNode("NewNode2");
        System.out.println("created");
    } catch (XMPPException e) {
        System.out.println("nothing created");
        e.printStackTrace();
    }




    // you could use publish() for asynchronous call

//      try {
//          myNode.send(item);
//          System.out.println("versendet");
//      } catch (XMPPException e) {
//          System.out.println("wird nicht gesendet");
//          e.printStackTrace();
//      }



}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} `

我的应用程序在 VM 2.3.3 上运行,因此不应该有任何线程问题。

这是错误日志

07:20:48 PM RCV (1079090656): <iq type="error" id="s1YUr-9" from="pubsub.nils-lappi" to="tester@nils-lappi/Smack"><pubsub xmlns="http://jabber.org/protocol/pubsub"><create node="NewNode2"/></pubsub><error code="409" type="cancel"><conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

所以我不知道为什么会有冲突,但似乎其余的都可以

由于冲突的错误代码: at org.jivesoftware.smackx.pubsub.packet.SyncPacketSend.getReply(SyncPacketSend.java:53)

希望这里有人可以帮助我...

4

1 回答 1

0

节点NewNode2已经存在,您正在尝试再次创建它。

注意:您可以将 PubsubManager 的创建简化为

new PubsubManager(connection);

使用pubsub作为服务名其实是默认的。

于 2012-12-04T14:20:19.230 回答