简体   繁体   English

通过Android连接到Openfire服务器

[英]Connect to Openfire server via Android

I'm trying to connect to my running Openfire server with this code: 我正在尝试使用以下代码连接到正在运行的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 logcat的

http://pastebin.com/9fcbzqgj http://pastebin.com/9fcbzqgj

EDIT 编辑

i added an exceptionininitializer exception and now it doesn't crash, but now i get a: 我添加了一个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 UPDATE

i've read that Android only allows access to some of the standard JRE classes, which are in this whitelist https://developers.google.com/appengine/docs/java/jrewhitelist 我了解到,Android仅允许访问某些标准JRE类,这些类在此白名单中https://developers.google.com/appengine/docs/java/jrewhitelist

so.. what should i do if i want to use the Smack api? 所以..如果我想使用Smack api,该怎么办? I can't? 我不能吗 I tried with aSmack, but it has to be compiled on linux or mac, and i only got windows 我尝试了aSmack,但必须在linux或mac上编译,而且我只有Windows

Please if you are using the smack api then along with this api you must also use the xpp.jar into your project. 请如果您使用的是smack api,则还必须与此API一起使用xpp.jar到您的项目中。 I also faced up the same issue and solved this problem with this api. 我也遇到了同样的问题,并使用此api解决了这个问题。

here is the link for that api. 这是该api的链接。 Xpp download link Xpp下载链接

In Android studio gradle, dependencies working for me: 在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