以下是代码:
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. */@OverrIDepublic 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();}}
请任何人都可以提供帮助.
解决方法 如果您在本地运行服务器,那么您应该只将主机设置为localhost,但是需要将服务(即XMPP域)设置为服务器上配置的任何服务.它不会是@domain,它将是域名. 总结以上是内存溢出为你收集整理的无法使用Android中的XMPP客户端登录ejabberd服务器全部内容,希望文章能够帮你解决无法使用Android中的XMPP客户端登录ejabberd服务器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)