подключение к серверу xmpp в android - PullRequest
0 голосов
/ 25 августа 2018

Я хочу, чтобы мое приложение отправляло и рецессивные сообщения, используя сервер 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) {

      }

пожалуйста, мне действительно нужно знать, правильный ли мой код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...