Android Powerful Development Support Library Components Appfromwork Framework Detailed
: Http://pan.baidu.com/s/1bbeOI
You browse friends, if you need to reprint, please indicate the source, whether the person blocked
Appfromwork is a multi-support app common development component, you can develop Android applications faster, Appfromwork can help you solve a lot of problems in development, but also can greatly improve your development rate, You can also support your two-time development of your own generic app development integration components.
In the previous article we talked about some other features, this time we'll focus on the development of XMPP instant messaging,
Let's look at the XMPP development support provided by the framework:
Let's start by saying how these classes work together:
The client sends a connection request to the server through the Xmppmanager class. The result of the framework return connection in the Xmpplistener listener, different connection methods are called by different results of the connection. When the successful connection, the client again to send a login request, the success of the login will return to the Xmpplistener listener, when the user login successfully, through MessageManager send a chat message to a user, When the client receives the message, it calls back Xmpplistener's ProcessMessage () method to get the message and the message content. Display to the screen to complete the instant messaging development.
I'll go through some of the methods of each key point and how to use the framework to complete the related development of XMPP.
1. How to connect to the server:
1 Manager.setxmpphost ("192.168.2.111"); Set the IP address of the server 2 manager.setxmppport (5222); Set the server's port 3 manager.setlistener (new Myxmpplistener ()); Set monitoring,4 manager.connection (); Connection method
As you can see, just set the server's IP address and port number. You can call the connection () method to implement the connection. The listener must be set before the connection method. When the connection succeeds, it will callback the XMPP connection listening method
1 Public void onconnsuccess (); Connection succeeded 2public void onconnerror ();
2. User login:
1 Manager.setusername (""); Set user name 2 manager.setpassword (""); Set Password 3 manager.setservicename ("");//Set server name 4 manager.login () ; Landing
Before landing, you need to set the user name, password, server Name property, set up the login () method to complete the login request. The results of the landing xmpplistener will be different depending on the result of the callback method.
1 Public void onloginsuccess (string Username, string password);//When the login is successful 2 Public void Onloginerror (string Username, string password); When the login fails
3. Create Friends and group information
Rostermanager manager = rostermanager.getinstance (); Get the Buddy Manager name manager.creategroup ("haha"); Create grouping information. Manager.addfriend ("lsq", "Liuschi", "haha"); Add friends to Group
Parameter description:
Manager.creategroup ("haha"); method incoming is the name of the group
Manager.addfriend (User,name, groupName); User: The username of the other party. Name : groupName: Group name
When a friend receives your add request, it processes your add request and adds whether the success will callback the Xmpplistener class:
/** * Each time you communicate with the server callback this method, can be used to verify the packet and processing the data returned by the server, processing friend requests and so on @param packet to the server communication packets */ Public void Interceptpacket (Packet Packet) { System.out.println (Packet.toxml ()); }
As to how you can tell if the other person has agreed to your add request, that's the XMPP protocol, I'm not going to say much here, XMPP will return different information based on different results, I'll just get the information and return to the listener's corresponding method.
4. Send chat messages.
The Send Chat message is divided into the following steps to complete.
1. Use the Chatmessage class to encapsulate the information you want to send, call the appropriate construction method to construct a chat message, which supports text, emoticons, pictures, files, voice and other messages.
2. Create a chat with the MessageManager class and send the chat message
3. The other party will callback Xmpplistener's ProcessMessage () to process the chat message when receiving your chat content
I'll explain each of the steps below:
4.1. Use the Chatmessage class to encapsulate the information you want to send, call the appropriate construction method to construct a chat message, which supports text, emoticons, pictures, files, voice and other messages.
Open the class and we can see the following four construction methods:
1 Public // construct an expression 2 Public Chatmessage (byte[] bytes, String type,string format) //construct a picture, file information 3 public chatmessage (String path,string format)//construct a voice message
4 public chatmessage (String content) //construct a text message
As for the relevant parameter description, I have already written in the comments of this class in succession.
4.2. Create a chat with the MessageManager class and send the chat message
To create a chat device:
Public void createchat (String to)
Parameter description: To: The user name of the other person you are chatting with
Send message:
Public void sendMessage (chatmessage message)
Parameter description: Message: The chat message has been packaged with Chatmessage.
4.3 Receive the message of the other party reply
By observing the relevant code of MessageManager, we know:
1 Private Final classMymessagelistenerImplementsmessagelistener{2 @Override3 Public voidProcessMessage (chat chat,4 org.jivesoftware.smack.packet.Message msg) {5 System.out.println (Msg.toxml ());6Chatmessage message =Chatmessage.fromjson (Msg.getbody ());7 if(Listener! =NULL)8 listener.processmessage (chat,msg,message);9 }Ten}
The Listener's ProcessMessage () method is recalled when the other person replies to the message. Processing the information of the other person's reply,
Description of the set of frames: The framework provides JSON encapsulation of chat information, because the underlying is a bunch of strings. So no matter what information you send will be sent to a string to send to each other, the other party needs to deal with the string after the related logical processing, below I for each kind of message to explain:
Text message: Chat content------>json package------> Send to another client
Picture information: Get the absolute path of the picture you want to send-----> Convert to byte[by Bytearrayoutputstream]---------> then encode byte[] into a string by Base64---> JSON package----> Send to another client
Expression information: Gets the ID information of the expression in the expression library---> convert the ID information through the string.valueof () method to a string----->json package------> Send another client
Voice message/File information: sent to another client as image information.
Well, the said I also said, if you read the article, or there is no understanding of the place. QQ Group can add: 435850465, the group will be seen in the future to the questions raised by the people to respond. Welcome to browse
About the use of appfromwork integrated XMPP development