2013-03-24 113 views
4

我正嘗試連接&使用Android中的XMPP客戶端登錄ejabberd服務器。 XMPP客戶端連接到服務器但未登錄。我收到異常消息爲服務器沒有響應。我不知道問題在哪裏。無法使用Android中的XMPP客戶端登錄ejabberd服務器

以下是代碼:

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 = "[email protected]";//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(); 
} 
} 

請會有人可以提供幫助。

+0

我應該爲主機地址輸入什麼內容?當我進入本地主機時,它不會連接。 ejabberd服務器作爲應用程序在本地機器上運行。 – 2013-03-25 13:14:53

+0

我希望主機地址10.0.2.2不是問題所在。 – 2013-03-25 13:31:24

回答

4

如果你是在本地運行的服務器,那麼你應該只設置主機本地主機,但需要該服務(即XMPP域)設置爲不管它被配置爲在服務器上。它不會是@域,它將是

+0

它不工作。當我使用10.0.2.2而不是localhost和@domain而不是域時,它可以工作。否則不是。問題是它沒有登錄,只能連接到服務器。請如果你能幫我登錄。我得到XMPPException,消息是沒有來自服務器的響應。 – 2013-03-25 19:14:28

+1

是10.0.2.2你的本地IP地址嗎?如果是,那麼應該像你所看到的那樣連接你,如果沒有,那麼你連接到其他地方的XMPP服務器。 @domain雖然不是您的服務,但您不包含@符號。 – Robin 2013-03-25 19:21:36

+0

localhost無法正常工作。所以我使用我的本地IP地址。 – 2013-03-26 12:30:00

相關問題