2014-01-15 48 views
1

這可能是重複的問題。從Android連接到XMPP服務器

我正在使用org.jivesoftware.smack for android作爲XMPP客戶端進行連接。而我的代碼是

ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE); 
XMPPConnection connection = new XMPPConnection(connConfig); 
try { 
    connection.connect(); 
    connection.login(USERNAME, PASSWORD); 
    Presence presence = new Presence(Presence.Type.available); 
    connection.sendPacket(presence); 
    setConnection(connection); 
} catch (XMPPException ex) {  
    ex.printStackTrace(); 
} 

我'得到一個例外,如下

01-18 16:20:36.021: W/System.err(13651): No response from the server.: 
01-18 16:20:36.021: W/System.err(13651): at org.jivesoftware.smack.SASLAuthentication.bindResourceAndEstablishSession(SASLAuthentication.java:307) 
01-18 16:20:36.029: W/System.err(13651): at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:214) 
01-18 16:20:36.029: W/System.err(13651): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:341) 
01-18 16:20:36.029: W/System.err(13651): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:301) 
01-18 16:20:36.029: W/System.err(13651): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:283) 
01-18 16:20:36.029: W/System.err(13651): at com.example.chatsample.XMPPClient.setupConnection(XMPPClient.java:112) 

回答

1
use this code for making connection with xmmp 


public static final String HOST = "talk.google.com"; 
    public static final int PORT = 5222; 
    public static final String SERVICE = "gmail.com"; 


public void connect() { 



     Thread t = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       // Create a connection 
       ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE); 
       XMPPConnection connection = new XMPPConnection(connConfig); 

       try { 
        connection.connect(); 
        Log.i("XMPPChatDemoActivity", 
          "Connected to " + connection.getHost()); 
       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to connect to "+ connection.getHost()); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 
       try { 
        // SASLAuthentication.supportSASLMechanism("PLAIN", 0); 
        connection.login(USERNAME, PASSWORD); 
        Log.i("XMPPChatDemoActivity","Logged in as " + connection.getUser()); 

        // Set the status to available 
        Presence presence = new Presence(Presence.Type.available); 
        connection.sendPacket(presence); 
        setConnection(connection); 

        if(connection.isConnected()) 
        { 


        Roster roster = connection.getRoster(); 
        Collection<RosterEntry> entries = roster.getEntries(); 
        int i=0; 
        for (RosterEntry entry : entries) 
        { 

         Presence entryPresence = roster.getPresence(entry.getUser()); 
         String presenceType=entryPresence.getType().toString(); 



         Presence.Type type = entryPresence.getType();      

         Log.d("XMPPChatDemoActivity","Type: " + entry.getType()); 
         entry.getType(); 
         entryPresence.getStatus(); 
         entryPresence.getType(); 
         Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus()); 
         Log.d("XMPPChatDemoActivity", "Presence Type: "+ entryPresence.getType()); 
         if (type == Presence.Type.available) 
          Log.d("XMPPChatDemoActivity", "Presence AVIALABLE"); 
         Log.d("XMPPChatDemoActivity", "Presence : "+ entryPresence);     

         if (type == Presence.Type.available) 
         { 
          Log.d("XMPPChatDemoActivity","--------------------------------------"); 
          Log.d("XMPPChatDemoActivity","online user Name: " + entry.getName()); 
          Log.d("XMPPChatDemoActivity","online user Name: " + entry.getUser()); 

          Log.d("XMPPChatDemoActivity","--------------------------------------"); 

          onlineuseremail.add(entry.getUser()); 
          onlineuserlist.add(entry.getUser()); 

          Log.d("XMPPChatDemoActivity","--------------------emaill------------"+onlineuseremail.get(i++)); 


         } 

        } 
        } 

       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to log in as "+ USERNAME); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 

       //    dialog.dismiss(); 
      } 
     }); 
     t.start(); 

    } 
+0

首先我無法連接。該代碼看起來相同,它的工作爲Gmail帳戶。 但我有一個服務器端口5222上運行1xx.1xx.x.x端口。我想連接到。 – Sundeep1501

1

這是我用來連接到我的XMPP服務器(在ejabberd)。

//... 
    SmackAndroid.init(context); 
    AndroidConnectionConfiguration connectionConfiguration = new AndroidConnectionConfiguration(ip_server, 5222,ip_server); 

    connectionConfiguration.setReconnectionAllowed(true); 
    connectionConfiguration.setSASLAuthenticationEnabled(true); 
    connectionConfiguration.setCompressionEnabled(true); 
    connectionConfiguration.setSecurityMode(SecurityMode.required); 
    connectionConfiguration.setTruststoreType("AndroidCAStore"); 
    connectionConfiguration.setTruststorePassword(null); 
    connectionConfiguration.setTruststorePath(null); 

    mConnection = new XMPPConnection(connectionConfiguration); 

    mConnection.connect(); 

    TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    mConnection.login(user, pass,"SMACK-"+telephonyManager.getDeviceId()); 
//... 

希望它可以幫助

2

爲了從Android XMPP客戶端連接到本地XMPP服務器。您需要:

  • 正確Asmack罐 - 下載here
  • 正確的參數ConnectionConfiguration - 主機,端口&服務器
  • 正確的登錄信息

ConnectionConfiguration了conconfig =新ConnectionConfiguration(主機,港口,服務);

  • 主持人:如果您的計算機安裝了XMPP服務器上同時使用Android設備&當地的WIFI。您必須使用您的本地無線IP地址作爲主機。注意:不是以太網IP &不是外部IP地址。

  • 港將保持5222

  • 服務器將是你在安裝您的XMPP服務器創建的服務器實例。

注意:確保當你試圖連接的服務器已啓動。

如果傳遞正確的參數ConnectionConfiguration,你將能夠連接如下:

XMPPConnection連接=新XMPPConnection(connConfig);
connection.connect();

問鼎連接,則可以登錄作爲用戶:

connection.login(用戶名,密碼);

注意:用戶名不需要服務器名稱。它可以簡單地管理

如果你想將消息發送給使用類似的其他用戶:

信息MSG =新的Message(receiverUserID,Message.Type。聊天);
msg.setBody(text);
connection.sendPacket(msg);

確保receiverUserID具有@serverInstanceName作爲user2 @ serverInstanceName等用戶標識的後綴。

注意:我用Ejabberd 2.1.11

TIP:如何創建一個Android XMPP客戶端的詳細解釋和如何將本地XMPP服務器在此link提供完整source code連接。

相關問題