4

AndroidのXMPPクライアントを使用してejabberdサーバーに接続してログインしようとしています。XMPPクライアントはサーバーに接続しますが、ログインしません。サーバーからの応答がないという例外メッセージが表示されます。どこに問題があるのか​​わかりません。

コードは次のとおりです。

XMPP Client.java

package org.apache.android.xmpp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import java.util.ArrayList;

public class XMPPClient extends Activity {

private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private SettingsDialog mDialog;
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection connection;

/**
 * Called with the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i("XMPPClient", "onCreate called");
    setContentView(R.layout.main);

    mRecipient = (EditText) this.findViewById(R.id.recipient);
    Log.i("XMPPClient", "mRecipient = " + mRecipient);
    mSendText = (EditText) this.findViewById(R.id.sendText);
    Log.i("XMPPClient", "mSendText = " + mSendText);
    mList = (ListView) this.findViewById(R.id.listMessages);
    Log.i("XMPPClient", "mList = " + mList);
    setListAdapter();

    // Dialog for getting the xmpp settings
    mDialog = new SettingsDialog(this);

    // Set a listener to show the settings dialog
    Button setup = (Button) this.findViewById(R.id.setup);
    setup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            mHandler.post(new Runnable() {
                public void run() {
                    mDialog.show();
                }
            });
        }
    });

    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String to = mRecipient.getText().toString();
            String text = mSendText.getText().toString();

            Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            connection.sendPacket(msg);
            messages.add(connection.getUser() + ":");
            messages.add(text);
            setListAdapter();
        }
    });
}

/**
 * Called by Settings dialog when a connection is establised with the XMPP server
 *
 * @param connection
 */
public void setConnection
        (XMPPConnection
                connection) {
    this.connection = connection;
    if (connection != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                    messages.add(fromName + ":");
                    messages.add(message.getBody());
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                        }
                    });
                }
            }
        }, filter);
    }
}

private void setListAdapter
        () {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item,
            messages);
    mList.setAdapter(adapter);
}
}

SettingDialog.java

package org.apache.android.xmpp;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

/**
 * Gather the xmpp settings and create an XMPPConnection
 */
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
private XMPPClient xmppClient;

public SettingsDialog(XMPPClient xmppClient) {
    super(xmppClient);
    this.xmppClient = xmppClient;
}

protected void onStart() {
    super.onStart();
    setContentView(R.layout.settings);
    getWindow().setFlags(4, 4);
    setTitle("XMPP Settings");
    Button ok = (Button) findViewById(R.id.ok);
    ok.setOnClickListener(this);
}

public void onClick(View v) {
    String host = "10.0.2.2";//getText(R.id.host);
    String port = "5222";//getText(R.id.port);
    String service = "@domain";//getText(R.id.service);
    final String username = "userid@domain";//getText(R.id.userid);
    final String password = "password";//getText(R.id.password);

    // Create a connection
    ConnectionConfiguration connConfig =
            new ConnectionConfiguration(host, Integer.parseInt(port), service);
    final XMPPConnection connection = new XMPPConnection(connConfig);

    new Thread(new Runnable()
    {
        public void run()
        {
            try {
                connection.connect();
                Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
            } catch (XMPPException ex) {
                Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
                Log.e("XMPPClient", ex.toString());
                xmppClient.setConnection(null);
            }
            try {
                connection.login(username, password);
                Log.i("XMPPClient", "Logged in as " + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                xmppClient.setConnection(connection);
            } catch (XMPPException ex) {
                Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
                Log.e("XMPPClient", ex.toString());
                    xmppClient.setConnection(null);
            }
        }
    }).start();

    dismiss();
}

private String getText(int id) {
    EditText widget = (EditText) this.findViewById(id);
    return widget.getText().toString();
}
}

誰でも手伝ってくれませんか。

4

1 に答える 1

5

サーバーをローカルで実行している場合は、ホストをlocalhostに設定する必要がありますが、サービス(つまり、XMPPドメイン)は、サーバー上で構成されているものに設定する必要があります。@domainではなく、 domainになります。

于 2013-03-25T15:57:20.203 に答える