Recently, I need to develop some projects related to instant messaging, and spent a few days collecting information about instant messaging.
Finally, openfire is selected as the server, and asmack is used as the android implementation.
1. Only send and not receive
If you follow the instructions written on the API, add the listener directly after the chat between new and a user.
Write as follows to solve the problem.
[Java]
View plaincopyprint?
- Chatmanager CM = conn. getchatmanager ();
- Chat newchat = cm. createchat (
- "Hanchenxi @ workgroup", null );
- Cm. addchatlistener (New chatmanagerlistener (){
- @ Override
- Public void chatcreated (chat arg0, Boolean arg1 ){
- Arg0.addmessagelistener (New messagelistener (){
- @ Override
- Public void processmessage (chat arg0, message arg1 ){
- If (arg1.getfrom (). Contains ("")){
- }
- Log. I ("Receive message", arg1.getbody ());
- }
- });
- }
- });
2. The key certificate cannot be found.
Add it to the connection configuration.
[Java]
View plaincopyprint?
- Connectionconfiguration connconfig = new connectionconfiguration ("192.168.1.116", 5222 );
- Connconfig. settruststorepath ("/system/etc/security/cacerts. BKS ");
- Connconfig. settruststoretype ("BKS ");
- Con = new xmppconnection (connconfig );
- Con. Connect ();
In October 20, another method was added to support more than 4.0 of systems.
[Java]
View plaincopyprint?
- Try {
- Connectionconfiguration connconfig = new connectionconfiguration (
- Config. getstring ("xmpptools. serveraddress"), 5222); // $ NON-NLS-1 $
- Log. I ("current operating system version API level =", build. version. sdk_int + ""); // $ NON-NLS-1 $ // $ NON-NLS-2 $
- If (build. version. sdk_int> = 14 ){
- Connconfig. settruststoretype ("androidcastore"); // $ NON-NLS-1 $
- Connconfig. settruststorepassword (null );
- Connconfig. settruststorepath (null );
- } Else {
- Connconfig. settruststoretype ("BKS"); // $ NON-NLS-1 $
- String Path = system. getproperty ("javax.net. SSL. truststore"); // $ NON-NLS-1 $
- If (Path = NULL)
- Path = system. getproperty ("Java. Home") + file. Separator // $ NON-NLS-1 $
- + "Etc" + file. Separator + "security" // $ NON-NLS-1 $ // $ NON-NLS-2 $
- + File. Separator + "cacerts. BKS"; // $ NON-NLS-1 $
- Connconfig. settruststorepath (PATH );
- }
- // Connconfig. setsaslauthenticationenabled (false );
- Connconfig. setreconnectionallowed (true );
- Connconfig. setsecuritymode (securitymode. Disabled );
- Con = new xmppconnection (connconfig );
- Con. Connect ();
3. Network exceptions
Before the connection
[Java]
View plaincopyprint?
- {
- Java. Lang. system. setproperty ("java.net. preferipv4stack", "true ");
- Java. Lang. system. setproperty ("java.net. preferipv6addresses ",
- "False ");
- }
4. File Transfer
Modify the source code package org. jivesoftware. smackx. filetransfer. socks5transfernegotiator. discoverlocalip ().
[Java]
View plaincopyprint?
- Private string discoverlocalip () throws unknownhostexception {
- Try {
- For (enumeration <networkinterface> en = networkinterface. getnetworkinterfaces (); en. hasmoreelements ();){
- Networkinterface INTF = en. nextelement ();
- For (enumeration <inetaddress> enumipaddr = INTF. getinetaddresses (); enumipaddr. hasmoreelements ();){
- Inetaddress = enumipaddr. nextelement ();
- If (! Inetaddress. isloopbackaddress ()){
- Return inetaddress. gethostaddress (). tostring ();
- }
- }
- }
- } Catch (socketexception ex ){
- Logger. Error ("error retrieving the local IP", ex );
- }
- Throw new unknownhostexception ("failed to retrieve local IP ");
- // Return inetaddress. getlocalhost (). gethostaddress ();
- }
So much for now.
Site: http://blog.csdn.net/yaeio/article/details/7906943
In particular, the Authentication Settings When configuring configuaration are as follows:
Connconfig. setsaslauthenticationenabled (false );
The default value of this attribute is true. The setting must be consistent with that on the server. If they are inconsistent, the server-unavailable (503) error will be returned even after the user is successfully registered, we use the ejabberd server. SASL authentication is enabled by default. Therefore, when I set it to false, I cannot log on. Finally, comment out the code and log on successfully :)