package com.hyl.openfire;
import java.util.Collection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private int PORT=5222;
private static Boolean isLogin=false;
XMPPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initConnection();
login("hyl1234","hyl1234");
getFriends();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initConnection(){
ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.104", PORT);
config.setSASLAuthenticationEnabled(false);
connection = new XMPPConnection(config);
try {
connection.connect();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void login(String name,String pass){
try {
connection.login(name, pass);
System.out.println("登录成功");
isLogin=true;
} catch (XMPPException e) {
System.out.println("登录失败");
e.printStackTrace();
}
}
public void getFriends(){
Roster roster = connection.getRoster();
Collection entriesGroup = roster.getGroups();
System.out.println("size--->"+entriesGroup.size());
for(RosterGroup group: entriesGroup){
Collection entries = group.getEntries();
System.out.println("group-->"+group.getName());
for (RosterEntry entry : entries) {
System.out.println("name-->"+entry.getName());
}
}
}
}