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>