XMPP protocol asmack Android client bug Solution

Source: Internet
Author: User

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?
  1. Chatmanager CM = conn. getchatmanager ();
  2. Chat newchat = cm. createchat (
  3. "Hanchenxi @ workgroup", null );
  4. Cm. addchatlistener (New chatmanagerlistener (){
  5. @ Override
  6. Public void chatcreated (chat arg0, Boolean arg1 ){
  7. Arg0.addmessagelistener (New messagelistener (){
  8. @ Override
  9. Public void processmessage (chat arg0, message arg1 ){
  10. If (arg1.getfrom (). Contains ("")){
  11. }
  12. Log. I ("Receive message", arg1.getbody ());
  13. }
  14. });
  15. }
  16. });

2. The key certificate cannot be found.

Add it to the connection configuration.

[Java]
View plaincopyprint?
  1. Connectionconfiguration connconfig = new connectionconfiguration ("192.168.1.116", 5222 );
  2. Connconfig. settruststorepath ("/system/etc/security/cacerts. BKS ");
  3. Connconfig. settruststoretype ("BKS ");
  4. Con = new xmppconnection (connconfig );
  5. Con. Connect ();

In October 20, another method was added to support more than 4.0 of systems.

[Java]
View plaincopyprint?
  1. Try {
  2. Connectionconfiguration connconfig = new connectionconfiguration (
  3. Config. getstring ("xmpptools. serveraddress"), 5222); // $ NON-NLS-1 $
  4. Log. I ("current operating system version API level =", build. version. sdk_int + ""); // $ NON-NLS-1 $ // $ NON-NLS-2 $
  5. If (build. version. sdk_int> = 14 ){
  6. Connconfig. settruststoretype ("androidcastore"); // $ NON-NLS-1 $
  7. Connconfig. settruststorepassword (null );
  8. Connconfig. settruststorepath (null );
  9. } Else {
  10. Connconfig. settruststoretype ("BKS"); // $ NON-NLS-1 $
  11. String Path = system. getproperty ("javax.net. SSL. truststore"); // $ NON-NLS-1 $
  12. If (Path = NULL)
  13. Path = system. getproperty ("Java. Home") + file. Separator // $ NON-NLS-1 $
  14. + "Etc" + file. Separator + "security" // $ NON-NLS-1 $ // $ NON-NLS-2 $
  15. + File. Separator + "cacerts. BKS"; // $ NON-NLS-1 $
  16. Connconfig. settruststorepath (PATH );
  17. }
  18. // Connconfig. setsaslauthenticationenabled (false );
  19. Connconfig. setreconnectionallowed (true );
  20. Connconfig. setsecuritymode (securitymode. Disabled );
  21. Con = new xmppconnection (connconfig );
  22. Con. Connect ();

 

3. Network exceptions

Before the connection

[Java]
View plaincopyprint?
  1. {
  2. Java. Lang. system. setproperty ("java.net. preferipv4stack", "true ");
  3. Java. Lang. system. setproperty ("java.net. preferipv6addresses ",
  4. "False ");
  5. }

4. File Transfer

Modify the source code package org. jivesoftware. smackx. filetransfer. socks5transfernegotiator. discoverlocalip ().

[Java]
View plaincopyprint?
  1. Private string discoverlocalip () throws unknownhostexception {
  2. Try {
  3. For (enumeration <networkinterface> en = networkinterface. getnetworkinterfaces (); en. hasmoreelements ();){
  4. Networkinterface INTF = en. nextelement ();
  5. For (enumeration <inetaddress> enumipaddr = INTF. getinetaddresses (); enumipaddr. hasmoreelements ();){
  6. Inetaddress = enumipaddr. nextelement ();
  7. If (! Inetaddress. isloopbackaddress ()){
  8. Return inetaddress. gethostaddress (). tostring ();
  9. }
  10. }
  11. }
  12. } Catch (socketexception ex ){
  13. Logger. Error ("error retrieving the local IP", ex );
  14. }
  15. Throw new unknownhostexception ("failed to retrieve local IP ");
  16. // Return inetaddress. getlocalhost (). gethostaddress ();
  17. }

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 :)

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.