繁体   English   中英

通过Android连接到Openfire服务器

[英]Connect to Openfire server via Android

我正在尝试使用以下代码连接到正在运行的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>

UPDATE

我了解到,Android仅允许访问某些标准JRE类,这些类在此白名单中https://developers.google.com/appengine/docs/java/jrewhitelist

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

请如果您使用的是smack api,则还必须与此API一起使用xpp.jar到您的项目中。 我也遇到了同样的问题,并使用此api解决了这个问题。

这是该api的链接。 Xpp下载链接

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM