【问题标题】:Connect to Openfire server via Android通过 Android 连接到 Openfire 服务器
【发布时间】:2023-03-26 18:20:02
【问题描述】:

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

编辑

我添加了一个异常初始化程序异常,现在它没有崩溃,但现在我得到了:

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

【问题讨论】:

标签: java android xmpp openfire


【解决方案1】:

如果您使用的是 smack api,那么除了这个 api,您还必须在您的项目中使用 xpp.jar。我也遇到了同样的问题,用这个 api 解决了这个问题。

这里是那个 api 的链接。 Xpp download link

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2015-03-13
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 2013-05-29
      • 1970-01-01
      相关资源
      最近更新 更多