Android instant chat tool YQ: (4) getting friends list

Source: Internet
Author: User

There is another important thing before chatting ,? That's right. It's a friend list. You don't know who you are talking to, right,

Hey hey, everything is simple and basic chat goals can be achieved earlier, so the code is very lazy (or rotten ?), Why? After reading it, you will know,

After a successful login on the server side, a new thread is opened for the server to communicate with this account, so that the server side can process other login requests,

[Java]
If (u. getOperation (). equals ("login") {// log on
Int account = u. getAccount ();
Boolean B = new UserDao (). login (account, u. getPassword (); // connect to the database to verify the user
If (B ){
System. out. println (MyData. getDate () + "'" + account + "' is online! ");
M. setType (YQMessageType. SUCCESS); // return a successful login information package
Oos. writeObject (m );
ServerConClientThread cct = new ServerConClientThread (s); // open a single thread to keep the thread connected to the client
ManageServerConClient. addClientThread (u. getAccount (), cct );
Cct. start (); // start the thread that communicates with the client
} Else {
M. setType (YQMessageType. FAIL );
Oos. writeObject (m );
}
} Else if (u. getOperation (). equals ("register ")){
// Register
}
The thread from which the Server communicates with a client:
[Java] ad extends Thread {
Socket s;
Public ServerConClientThread (Socket s ){
This. s = s;
}
 
Public void run (){
While (true ){
ObjectInputStream ois = null;
YQMessage m = null;
Try {
Ois = new ObjectInputStream (s. getInputStream ());
M = (YQMessage) ois. readObject ();
// Determine the type of messages obtained from the client and process the messages accordingly
If (m. getType (). equals (YQMessageType. COM_MES) {// if it is a common message package
// Gets the recipient's communication thread
ServerConClientThread scc = ManageServerConClient. getClientThread (m. getReceiver ());
ObjectOutputStream oos = new ObjectOutputStream (scc. s. getOutputStream ());
// Send a message to the recipient
Oos. writeObject (m );
} Else if (m. getType (). equals (YQMessageType. GET_ONLINE_FRIENDS) {// if it is a list of requesting friends
// Operate the database. Here, the list of all users is returned. Leave it empty and add friends.
// Temporarily compress the result into the string type. You can change it to json later.
String res = new UserDao (). getUser ();
// Send a friend list to the client
ServerConClientThread scc = ManageServerConClient. getClientThread (m. getSender ());
ObjectOutputStream oos = new ObjectOutputStream (scc. s. getOutputStream ());
YQMessage MS = new YQMessage ();
Ms. setType (YQMessageType. RET_ONLINE_FRIENDS );
Ms. setContent (res );
Oos. writeObject (MS );
}
} Catch (Exception e ){
E. printStackTrace ();
Try {
S. close ();
Ois. close ();
} Catch (IOException e1 ){

}
}
}
}
}
You can see that the code for processing the list of friends in the request has been available. Now you know why the code is poorly written. Haha,
In the client, it is similar to the above:


[Java]
If (ms. getType (). equals (YQMessageType. SUCCESS )){
// Create a thread that maintains the connection between the account and the server
ClientConServerThread ccst = new ClientConServerThread (context, s );
// Start the communication thread
Ccst. start ();
// Add to the management class
ManageClientConServer. addClientConServerThread (User) obj). getAccount (), ccst );
B = true;
} Else if (ms. getType (). equals (YQMessageType. FAIL )){
B = false;
}
To add the thread connecting the client to the server to the ManageClientServer class, you can obtain the ObjectOutputStream object at any time to send data to the server. Of course, you can also write a thread dedicated to sending data.

 

After successfully logging on to the client, send a packet requesting the friend list to the server:

[Java]
// Send a Message requesting an online friend to be returned
ObjectOutputStream oos = new ObjectOutputStream (
ManageClientConServer. getClientConServerThread (user. getAccount (). getS (). getOutputStream ());
YQMessage m = new YQMessage ();
M. setType (YQMessageType. GET_ONLINE_FRIENDS );
M. setSender (user. getAccount ());
Oos. writeObject (m );

 

After you get the friends list, it will be OK when it is displayed in the friends list. listView and adapter will not be detailed,

Effect after completion

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.