· 7 years ago · Oct 07, 2018, 12:48 PM
1 String [] getfriend;
2getfriend = connobj.conn("poisonsaves@gmail.com", "********");
3--------------------------------------------------------------------------------------------------------------
4String[] conn(String Username,String pass){
5 String []friend = null;
6 try {
7 ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
8 //Creating a Connection Configuration object which defines parameter for connection
9 //Here Connection Configuration object is configured to connect with GTalk server
10 //Creating a connection object using the above created configuration
11
12 XMPPConnection connection = new XMPPConnection(connectionConfiguration);
13 System.out.println("Trying to connect");
14 //Calling connect tries to establish connection with XMPP server
15 //It throws XMPP Exception
16 connection.connect();
17 System.out.println("Connected");
18 //SASL = Simple Authentication and Security Layer
19 //Registers a new SASL mechanism in the specified preference position.
20 //The client will try to authenticate using the most prefered SASL mechanism
21 //that is also supported by the server.
22 SASLAuthentication.supportSASLMechanism("PLAIN", 0);
23 System.out.println("Trying to Log In");
24 //Calling login logs in to the XMPP server
25 //It throws XMPP Exception
26 connection.login(Username, pass);
27 System.out.println("Logged In");
28 //Roster means friend list
29 Roster roster = connection.getRoster();
30 //Making collection of RosterEntry
31 Collection<RosterEntry> rosterEntries = roster.getEntries();
32 int size = rosterEntries.size();
33 friend = new String[size] ;
34 if( rosterEntries.size()>0)
35 {
36 //Printing out name of each friend using iterator on RosterEntry collection
37 for(RosterEntry rosterEntry: rosterEntries) {
38 int i=0;
39 friend[i] = rosterEntry.getUser(); //return(rosterEntry.getUser());
40 //Disconnecting the XMPP Connection
41 //System.out.println(rosterEntry.getUser() + " " + friend[i]);
42 i++;
43 }
44
45 connection.disconnect();
46
47 }
48 else {
49 }
50
51
52
53 } catch (XMPPException e) {
54 e.printStackTrace();
55 //System.out.println("Problem connecting or logging in");
56 //System.exit(0);
57 }
58 return friend;
59
60 }