2014-04-01 47 views
-1

我想使用xmpp顯示facebook聊天,我想列出在線的朋友,然後使用xmpp.for聊天,這給出了很多錯誤;現在我把喜歡這個代碼,這是正確的代碼,然後我跟着這個鏈接ssl店http://randomizedsort.blogspot.in/2010/09/step-to-step-guide-to-programming.html和這個Using the Android TrustStore for aSmack in Android 4+ (ICS)也。使用xmpp問題的Facebook聊天-NoSuchAlgorithmException:KeyStore jks實現未找到

Button setup = (Button) this.findViewById(R.id.setup); 
     setup.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       mHandler.post(new Runnable() { 
        public void run() 
        { 
         // mDialog.show(); 
         loginfacebook(); 


        } 
       }); 
      } 
     }); 

public void loginfacebook() 
    { 
     Session currentSession = Session.getActiveSession(); 
     if (currentSession == null || currentSession.getState().isClosed()) { 

     System.out.println("your current fb session is closed........ or you are not logged in..............."); 
      Session session = new Session.Builder(XMPPClient.this).build(); 
      Session.setActiveSession(session); 
      currentSession = session; 
     } 

     if (currentSession.isOpened()) { 
      // Do whatever u want. User has logged in 
     System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr..............."); 
      Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() { 
     @SuppressWarnings("deprecation") 
     @Override 
      public void call(final Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 
       ////do after login work here.................... 
       // mDialog.show(); 

        System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken()); 
         ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
         config.setSASLAuthenticationEnabled(true); 
         config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
         XMPPConnection xmpp = new XMPPConnection(config); 
         SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
         SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 

         try { 
          xmpp.connect(); 


         } catch (XMPPException e1) { 
          Log.i("XMPPClient", 
            "Unable to " + xmpp.getHost()); 

          e1.printStackTrace(); 
         } 
         try { 
          xmpp.login(fb.getAppId(), session.getAccessToken()); 



         } catch (XMPPException e) { 
          e.printStackTrace(); 
         } 
       } 
     } 
      }); 
     } 

     else if (!currentSession.isOpened()) { 
      // Ask for username and password 
     System.out.println("your are going to ask for authenticationnnnnnnnnnnnn..............."); 
      OpenRequest op = new Session.OpenRequest((Activity) XMPPClient.this); 

      op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO); 
      op.setCallback(null); 

      List<String> permissions = new ArrayList<String>(); 
      // permissions.add("publish_stream"); 
      //permissions.add("user_likes"); 
      permissions.add("email"); 
      permissions.add("user_birthday"); 
      permissions.add("friends_birthday"); 
      op.setPermissions(permissions); 

      Session session = new Session.Builder(XMPPClient.this).build(); 
      Session.setActiveSession(session); 
      session.openForPublish(op); 
     } 
    } 

    public void call(Session session, SessionState state, Exception exception) 
    { 
    }  



     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    System.out.println("Face boookkk has returned valuessssssssssssssssssssssssss..............."); 
      super.onActivityResult(requestCode, resultCode, data); 
      if (Session.getActiveSession() != null) 
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 

      Session currentSession = Session.getActiveSession(); 

      if (currentSession == null || currentSession.getState().isClosed()) { 
      System.out.println("unable to create sessionnnnnnnnnnnn..............."); 
       Session session = new Session.Builder(XMPPClient.this).build(); 
       Session.setActiveSession(session); 
       currentSession = session; 
      } 

      if (currentSession.isOpened()) { 
      System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr..............."); 
      Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() { 
     @SuppressWarnings("deprecation") 
     @Override 
      public void call(final Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 
       // after login workkkkkkkkkkk 
        System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken()); 
      /*  ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
       config.setSASLAuthenticationEnabled(true); 
       config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
       XMPPConnection xmpp = new XMPPConnection(config); 
       SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
       SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);*/ 


      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
       ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
       config.setSASLAuthenticationEnabled(true); 
       config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
       XMPPConnection xmpp = new XMPPConnection(config); 
       SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
       SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 
       config.setTruststoreType("AndroidCAStore"); 
       config.setTruststorePassword(null); 
      config.setTruststorePath(null); 
      try { 
       xmpp.connect(); 


      } catch (XMPPException e1) { 
       Log.i("XMPPClient", 
         "Unable to " + xmpp.getHost()); 

       e1.printStackTrace(); 
      } 
      try { 
       xmpp.login(fb.getAppId(), session.getAccessToken()); 



      } catch (XMPPException e) { 
       e.printStackTrace(); 
      } 
      } else { 
       ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
       config.setSASLAuthenticationEnabled(true); 
       config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
       XMPPConnection xmpp = new XMPPConnection(config); 
       SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
       SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 
       config.setTruststoreType("BKS"); 
       String path = System.getProperty("javax.net.ssl.trustStore"); 
       if (path == null) 
        path = System.getProperty("java.home") + File.separator + "etc" 
         + File.separator + "security" + File.separator 
         + "cacerts.bks"; 
      config.setTruststorePath(path); 
      try { 
       xmpp.connect(); 


      } catch (XMPPException e1) { 
       Log.i("XMPPClient", 
         "Unable to " + xmpp.getHost()); 

       e1.printStackTrace(); 
      } 
      try { 
       xmpp.login(fb.getAppId(), session.getAccessToken()); 



      } catch (XMPPException e) { 
       e.printStackTrace(); 
      } 
      } 


       } 
     } 
      }); 
     } 
    } I am searching lot for connecting facebook xmpp not getting proper solutions. 

回答

0

對於錯誤,我用這樣的代碼,後搜查很多鏈接我得到它解決了我的問題,目前Successfull authentication but status is always "offline" and rooster is empty (Android,XMPP,Facebook)

我用這樣的代碼

if (currentSession.isOpened()) { 
      // Do whatever u want. User has logged in 
     System.out.println("your are already logged in userrrrrrrrrrrrrrrrrrrrrrrrrrr..............."); 
      Session.openActiveSession(XMPPClient.this, true, new Session.StatusCallback() { 
     @SuppressWarnings("deprecation") 
     @Override 
      public void call(final Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 
       ////do after login work here.................... 

        System.out.println("App id"+fb.getAppId() + "Access token"+ session.getAccessToken()); 

         ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
         config.setDebuggerEnabled(true); 
         config.setSASLAuthenticationEnabled(true); 
         config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
          config.setTruststoreType("AndroidCAStore"); 
          config.setTruststorePassword(null); 
          config.setTruststorePath(null); 
         } else { 
          config.setTruststoreType("BKS"); 
          String path = System.getProperty("javax.net.ssl.trustStore"); 
          if (path == null) 
           path = System.getProperty("java.home") + File.separator + "etc" 
            + File.separator + "security" + File.separator 
            + "cacerts.bks"; 
          config.setTruststorePath(path); 
         } 
         XMPPConnection xmpp = new XMPPConnection(config); 
         SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
         SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 
         Presence presence = new Presence(Presence.Type.subscribe); 
         presence.setStatus("Online"); 
         xmpp.sendPacket(presence); 
         System.out.println("PRE-CONNECTED"); 


         try { 
          xmpp.connect(); 


         } catch (XMPPException e1) { 
          Log.i("XMPPClient", 
            "Unable to " + xmpp.getHost()); 

          e1.printStackTrace(); 
         } 
         try { 
          xmpp.login(fb.getAppId(), session.getAccessToken()); 



         } catch (XMPPException e) { 
          e.printStackTrace(); 
         } 
       } 
     } 
      }); 
     } 
一個好臨客