无法使用XMPP客户端登录到服务器的ejabberd在Android中 [英] Unable to login to ejabberd server using XMPP client in Android

查看:516
本文介绍了无法使用XMPP客户端登录到服务器的ejabberd在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试连接和放大器;登录到使用XMPP客户端在Android中的ejabberd服务器。该XMPP客户端连接到服务器,但没有日志中。我收到异常消息为否从服务器响应。我不知道问题出在哪里。

I am trying to connect & login to ejabberd server using a XMPP client in Android. The XMPP client connects to the server but doesn't logs in. I am getting Exception message as No response from the server. I don't know where is the problem.

以下是code:

XMPP Client.java

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

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();
}
}

请会有人可以提供帮助。

Please could anyone can help.

推荐答案

如果您是在本地运行的服务器,那么你应该只设置主机为本地主机,但该服务(即XMPP域)需要被设置为不管它被配置成在服务器上。这不会是 @domain ,这将是

If you are running the server locally, then you should just set the host to localhost, but the service (i.e. XMPP domain) needs to be set to whatever it is configured to be on the server. It won't be @domain, it would be domain.

这篇关于无法使用XMPP客户端登录到服务器的ejabberd在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆