asmack stream error (host-unknown)
質問
I try to write an android demo with asmack. There is an error named “host-unknown”
. How can I solve that?
Here is what I see in 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)
My code:
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();
}
Edit2
The XML file code is very simple, only just contain a button.
My xml file code:
<?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>
解決
- First of all, I don't think your code can run, because after you check connection == null, you do not init connection, just use: connection = new XMPPConnection(config);
- I have met the situation of un-known host, the problem is that we do not specify the service name in the constructor of the ConnectionConfiguration (you specified it as an empty string). Normally, if user's email is of the form : user@something.com, the service name will be "something.com". Anyway, you can get the exact service name after you are connected to the server (after connection.connect() succeeds) by using xmpp.getServiceName() (You can log to see the result, for example: Log.e("Service Name", xmpp.getServiceName()), then see the Logcat and copy the service name into the constructor).
- Enjoyed. :)
所属していません StackOverflow