2014-05-07 30 views
1
的Openfire服務器

我想使用此代碼連接到我運行的Openfire服務器:連接到使用Android

public static final String HOST = "ipofmyserver"; 
public static final int PORT = 9122; // set by me 
//public static final String SERVICE = "gmail.com"; not used because i don't know what it refers to 

ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT); 
XMPPConnection connection = new XMPPConnection(connConfig); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Connect to the server 
    try { 
     connection.connect(); 
    } catch (XMPPException e) { 
     connection = null; 
     //Unable to connect to server 
    } 

    //Most servers require you to login before performing other tasks. 
    if (connection != null) { 
     try { 
      connection.login("xxxx", "xxxx"); 
     } catch (XMPPException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

logcat的

http://pastebin.com/9fcbzqgj

編輯

我添加了一個exceptionininitializer異常,現在它不會崩潰,但現在我得到一個:

05-07 03:27:55.734 3184-3184/xxx.xxx.xxxxxE/dalvikvm﹕ Could not find class 'javax.naming.directory.InitialDirContext', referenced from method org.jivesoftware.smack.util.dns.JavaxResolver.<clinit> 

UPDATE

我讀過的Android只允許一些標準JRE類,這是在這個白名單https://developers.google.com/appengine/docs/java/jrewhitelist

這樣的訪問..如果我想使用Smack API,我應該怎麼做?我不能?我試着用aSmack,但它必須在linux或mac上編譯,而且我只有windows

+0

可能重複(http://stackoverflow.com/questions/23507724/how -to-實現-XMPP的聊天功能於一,Android的應用程序) – Flow

回答

0

如果你使用的是smack api,那麼隨着這個api,你還必須在你的項目中使用xpp.jar。我也面臨同樣的問題,並用這個API解決了這個問題。

這裏是該API的鏈接。 Xpp download link

0

在Android中的gradle工作室,依賴關係爲我工作:[?如何實現Android應用中XMPP聊天]

dependencies { 
    compile 'org.igniterealtime.smack:smack-android:4.1.1' 
    compile 'org.igniterealtime.smack:smack-android-extensions:4.1.1' 
    compile 'org.igniterealtime.smack:smack-core:4.1.1' 
    compile 'org.igniterealtime.smack:smack-tcp:4.1.1' 
    compile 'org.igniterealtime.smack:smack-extensions:4.1.1' 
    compile 'org.igniterealtime.smack:smack-experimental:4.1.1' 
    compile 'org.igniterealtime.smack:smack-resolver-minidns:4.1.1' 
    compile 'org.igniterealtime.smack:smack-sasl-provided:4.1.1' 
    compile 'org.igniterealtime.smack:smack-im:4.1.1' 
    compile 'org.jxmpp:jxmpp-core:0.4.2-beta1' 
    compile 'org.jxmpp:jxmpp-util-cache:0.4.2-beta1' 
    compile 'de.measite.minidns:minidns:0.1.1' 
    compile 'com.android.support:appcompat-v7:22.2.0' 
}