通过Android连接到Openfire服务器

时间:2014-05-07 00:42:22

标签: java android xmpp openfire

我正在尝试使用以下代码连接到正在运行的Openfire服务器:

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>

更新

我读过Android只允许访问一些标准的JRE类,这些类在白名单中https://developers.google.com/appengine/docs/java/jrewhitelist

所以..如果我想使用Smack API我该怎么办?我不能?我尝试使用aSmack,但它必须在linux或mac上编译,而我只有windows

2 个答案:

答案 0 :(得分:0)

如果您正在使用smack api,请使用此api,您还必须将xpp.jar用于您的项目。我也遇到了同样的问题并用这个api解决了这个问题。

这是api的链接。 Xpp download link

答案 1 :(得分:0)

在Android studio gradle中,依赖项为我工作:

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'
}