使用Smack和Openfire调用Roster.createEntry()时出现“internal-server-error(500)”错误响应

时间:2014-01-13 13:01:35

标签: xmpp openfire smack asmack

您好我正在使用简单的OpenFire服务器将消息从Android设备发送给其他用户。我在android中使用asmack jar。这是我正在使用的类:

public class XmppManager {

private static final int packetReplyTimeout = 500; // millis

private String server;
private int port;

private ConnectionConfiguration config;
private XMPPConnection connection;

private ChatManager chatManager;
private MessageListener messageListener;

public XmppManager(String server, int port) {
    this.server = server;
    this.port = port;
}

public void init() throws XMPPException {


    System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));

    SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);


    config = new ConnectionConfiguration(server, port);
    config.setSASLAuthenticationEnabled(false);
    config.setSecurityMode(SecurityMode.disabled);

    connection = new XMPPConnection(config);
    connection.connect();

    System.out.println("Connected: " + connection.isConnected());

    chatManager = connection.getChatManager();
    messageListener = new MyMessageListener();

}

public void performLogin(String username, String password) throws XMPPException {
    if (connection!=null && connection.isConnected()) {
        connection.login(username, password);
    }
}

public void setStatus(boolean available, String status) {

    Presence.Type type = available? Type.available: Type.unavailable;
    Presence presence = new Presence(type);

    System.out.println("PRESENCE"+type+" "+available);

    presence.setPriority(1);
    presence.setStatus(status);
    connection.sendPacket(presence);

}

public void destroy() {
    if (connection!=null && connection.isConnected()) {
        connection.disconnect();
    }
}

public void sendMessage(String message, String buddyJID) throws XMPPException {
    System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
    Chat chat = chatManager.createChat(buddyJID, messageListener);
    chat.sendMessage(message);
}

public void createEntry(String user, String name) throws Exception {
    System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
    Roster roster = connection.getRoster();
    roster.createEntry(user, name, null);
}

class MyMessageListener implements MessageListener {

    @Override
    public void processMessage(Chat chat, Message message) {
        String from = message.getFrom();
        String body = message.getBody();
        System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
    }

}

 }

使用的活动是

     public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try{
    SmackAndroid.init(getApplicationContext());
    }catch(Exception e)
    {
        e.printStackTrace();


    }

    new XmppMessage().execute(); 
}

@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;
}

private class XmppMessage extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        try {
            XmppManager manager = new XmppManager("192.168.1.6", 5222);

            manager.init();
            manager.performLogin("admin", "root");
            manager.setStatus(true, "I am mobile");

            /* String buddyJID = "test@192.168.1.6";
                String buddyName = "test";
                manager.createEntry(buddyJID, buddyName);

                manager.sendMessage("Hello mate", "test@192.168.1.6");
                */
                boolean isRunning = true;

                while (isRunning) {
                    Thread.sleep(50);
                }

                manager.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

}

连接正确,并且在OpenFire管理控制台上显示在线,但是当我尝试向其他测试用户发送消息时,它给出了错误

  01-13 17:53:12.696: W/System.err(22946): internal-server-error(500)
  01-13 17:53:12.696: W/System.err(22946):  at org.jivesoftware.smack.Roster.createEntry(Roster.java:303)
  01-13 17:53:12.696: W/System.err(22946):  at com.twis.demochat.XmppManager.createEntry(XmppManager.java:91)
  01-13 17:53:12.706: W/System.err(22946):  at com.twis.demochat.MainActivity$XmppMessage.doInBackground(MainActivity.java:50)
  01-13 17:53:12.706: W/System.err(22946):  at com.twis.demochat.MainActivity$XmppMessage.doInBackground(MainActivity.java:1)
  01-13 17:53:12.706: W/System.err(22946):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
  01-13 17:53:12.706: W/System.err(22946):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  01-13 17:53:12.706: W/System.err(22946):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  01-13 17:53:12.706: W/System.err(22946):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
  01-13  17:53:12.716: W/System.err(22946):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  01-13 17:53:12.716: W/System.err(22946):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  01-13 17:53:12.716: W/System.err(22946):  at java.lang.Thread.run(Thread.java:864)

我尝试过使用smack.jar和asmack.jar。我也使用了AndroidSmack.init()但仍然是同样的错误。请帮助我对此不熟悉。

1 个答案:

答案 0 :(得分:0)

我也收到500错误,因为我以前登录的用户在服务器上不存在。