连接到Android中的xmpp服务器

时间:2018-08-25 17:27:38

标签: android android-studio xmpp

我想使我的应用程序能够使用xmpp服务器发送和隐性消息,我已经安装并配置了openfire,但是我的应用程序仍然无法通过xmpp服务器发送消息。

下面是我尝试使用的连接到xmpp服务器的代码

ServerConnection

public class ServerConnection implements ConnectionListener {


private static final String TAG = "ServerConnection";
public static XMPPTCPConnection connection;
private Context mApplicationContext = null;
private String mUsername = null;
private String mPassword = null;
private String mServiceName = null;
private XMPPTCPConnection mConnection;
private BroadcastReceiver uiThreadMessageReceiver;//Receives messages from the ui thread.

private final Set<ChatMessageListener> listeners = new CopyOnWriteArraySet<>();

public ServerConnection() {
}

public static enum ConnectionState

{
    CONNECTED ,AUTHENTICATED, CONNECTING ,DISCONNECTING ,DISCONNECTED;
}

public static enum LoggedInState
{
    LOGGED_IN , LOGGED_OUT;
}


public ServerConnection ( Context context){

    Log.d(TAG,"ServerConnection Constructor called.");
    mApplicationContext = context.getApplicationContext();
    String jid = PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
            .getString("xmpp_jid",null);
    mPassword = PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
            .getString("xmpp_password",null);

    if( jid != null)

    {
        mUsername = jid.split("@")[0];
        mServiceName = jid.split("@")[1];
    }else
    {
        mUsername ="";
        mServiceName="192.168.8.101";
    }
}


public void connect() throws IOException, XMPPException, SmackException,       InterruptedException, XmppStringprepException
   {
        Log.d(TAG, "Connecting to server " + mServiceName);
       DomainBareJid domainName = JidCreate.domainBareFrom("localhost");

       XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
               .setServiceName("192.168.8.101")
               .setHost("192.168.8.101")
               .setResource("Roster")

               //Was facing this issue
               //https://discourse.igniterealtime.org/t/connection-with-ssl-fails-with-java-security-keystoreexception-jks-not-found/62566
               .setKeystoreType(null) //This line seems to get rid of the problem

               .setSecurityMode(ConnectionConfiguration.SecurityMode.required)
               .setCompressionEnabled(true).build();

   //    Log.d(TAG, "Username : "+mUsername);
    //   Log.d(TAG, "Password : "+mPassword);
      // Log.d(TAG, "Server : "+mServiceName);


    //Set up the ui thread broadcast message receiver.
    setupUiThreadBroadCastMessageReceiver();

   // mConnection = new XMPPTCPConnection(config);
    mConnection.addConnectionListener(this);
    Log.d(TAG, "Calling connect() ");
    mConnection.connect();
    mConnection.login(mUsername,mPassword);
    Log.d(TAG, " login() Called ");

    org.jivesoftware.smack.chat2.ChatManager.getInstanceFor(mConnection).addIncomingListener(new IncomingChatMessageListener() {
        @Override
        public void newIncomingMessage(EntityBareJid entityBareJid, Message message, org.jivesoftware.smack.chat2.Chat chat) {

      }

请我真的需要知道我的代码是否正确。

0 个答案:

没有答案