gpt4 book ai didi

android - asmack 流错误(主机未知)

转载 作者:行者123 更新时间:2023-11-29 16:15:42 33 4
gpt4 key购买 nike

我尝试用 asmack 编写一个 android 演示。存在名为 “host-unknown” 的错误。我该如何解决?

这是我在 LogCat 中看到的:

03-16 08:31:10.671: I/getConnection(795): connect success
03-16 08:31:10.905: W/System.err(795): stream:error (host-unknown)
03-16 08:31:10.905: W/System.err(795): at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:261)
03-16 08:31:10.912: W/System.err(795): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
03-16 08:31:10.912: W/System.err(795): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)

我的代码:

private static String host = "172.16.131.99";
private static int port = 5222;

private Button btnLogin;

private static XMPPConnection connection = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (login()) {
showMessage("Login success");
} else {
showMessage("Login failure");
}
}

});
}

private XMPPConnection getConnection() {
if (connection == null) {
ConnectionConfiguration config = new ConnectionConfiguration(host, port, "");
// use TLS
config.setSecurityMode(SecurityMode.required);
try {
connection.connect();
Log.i("getConnection", "connect success");
} catch (Exception e) {
e.printStackTrace();
}
}

return connection;
}

private boolean login() {
try {
// login
getConnection().login("admin", "123456");
Log.i("Login", "login success");

// set status
Presence presence = new Presence(Presence.Type.available);
getConnection().sendPacket(presence);
Log.i("Login", "set status success");

return true;
} catch (Exception e) {
e.printStackTrace();
}

return false;
}

private void showMessage(String message) {
Toast.makeText(this, message, 1000).show();
}

编辑2

XML文件代码很简单,只包含一个按钮。

我的xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/login" />
</LinearLayout>

最佳答案

  1. 首先,我不认为你的代码可以运行,因为在你检查 connection == null 之后,你没有初始化连接,只是使用:connection = new XMPPConnection(config);
  2. 我遇到过未知主机的情况,问题是我们没有在 ConnectionConfiguration 的构造函数中指定服务名称(您将其指定为空字符串)。通常,如果用户的电子邮件格式为:user@something.com,则服务名称将为“something.com”。无论如何,您可以在连接到服务器后(在 connection.connect() 成功之后)通过使用 xmpp.getServiceName() (您可以登录以查看结果,例如: Log.e("Service Name", xmpp.getServiceName()), 然后查看 Logcat 并将服务名称复制到构造函数中)。
  3. 很享受。 :)

关于android - asmack 流错误(主机未知),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9734316/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com