AnsweredAssumed Answered

How Can I connect to my own ejabberd Server 2.1.11?

Question asked by gaurav3237 on Oct 8, 2014

I have installed a local ejabberd server 2.1.11 on windows.

I need to connect it using an Android Device.

I am using code available on stackoverflow but most of it seems to be old, according to smack 3.2.1.

Can someone give me details about how to do it using asamack 4.0.4 OR 4.1My Current Code is :

 

import java.io.File;

 

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;

import org.jivesoftware.smack.SASLAuthentication;

import org.jivesoftware.smack.SmackAndroid;

import org.jivesoftware.smack.SmackConfiguration;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.tcp.XMPPTCPConnection;

 

 

 

import android.app.Dialog;

import android.content.Context;

import android.os.AsyncTask;

import android.os.Build;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

 

/**

* Gather the xmpp settings and create an XMPPConnection

*/

public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {

    private XMPPClient xmppClient;

    private Context cxt;

    public SettingsDialog(XMPPClient xmppClient,Context cxt) {

        super(xmppClient);

        this.xmppClient = xmppClient;

        this.cxt = cxt;

       

    }

    private class AsyncMain extends AsyncTask<String, String, String> {

        

        // Show Progress bar before downloading Music

        @Override

        protected void onPreExecute() {

            super.onPreExecute();

            // Shows Progress Bar Dialog and then call doInBackground method

           

        }

 

        // Download Music File from Internet

        @Override

        protected String doInBackground(String... x) {

           

            try {

            dothis();

            } catch (Exception e) {

                // TODO Auto-generated catch block

                Log.i("pastry",e.getMessage());

                e.printStackTrace();

            }

            return null;

        }

 

        // While Downloading Music File

        protected void onProgressUpdate(String... progress) {

            // Set progress percentage

          

        }

 

        // Once Music File is downloaded

        @Override

        protected void onPostExecute(String file_url) {

            // Dismiss the dialog after the Music file was downloaded

           

        }

    }

    protected void onStart() {

        super.onStart();

        setContentView(R.layout.settings);

        getWindow().setFlags(4, 4);

        setTitle("XMPP Settings");

        Button ok = (Button) findViewById(R.id.ok);

        ok.setOnClickListener(this);

        setText(R.id.host,"10.0.2.2");

        setText(R.id.port,"5222");

        setText(R.id.service,"domain");

        setText(R.id.userid,"admin@domain");

        setText(R.id.password,"password");

    }

 

    public void onClick(View v) {

        String[] x = {};

        new AsyncMain().execute(x);

    }

 

    public void dothis() {

        String host = getText(R.id.host);

        String port = getText(R.id.port);

        String service = getText(R.id.service);

        String username = getText(R.id.userid);

        String password = getText(R.id.password);

 

        // Create a connection

        SmackAndroid.init(cxt);

        SmackConfiguration.DEBUG_ENABLED = true;

        ConnectionConfiguration connConfig =

                new ConnectionConfiguration(host, Integer.parseInt(port), service);

        //connConfig.setSASLAuthenticationEnabled(true);

        //System.setProperty("smack.debugEnabled", "true");

        //XMPPConnection.DEBUG = true;

        //SASLAuthentication.supportSASLMechanism("PLAIN", 0);

        //connConfig.setCompressionEnabled(true);

        connConfig.setSecurityMode(SecurityMode.enabled);

 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)

        {

            connConfig.setKeystoreType("AndroidCAStore");

            //connConfig.setKeystorePassword(null);

            connConfig.setKeystorePath(null);

            Log.i("XMPPClient", "[XmppConnectionTask] Build Icecream");

 

        }

        else

        {

            connConfig.setKeystoreType("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";

            connConfig.setKeystorePath(path);

            Log.i("XMPPClient", "[XmppConnectionTask] Build less than Icecream ");

 

        }

        connConfig.setDebuggerEnabled(true);

        XMPPConnection connection = new XMPPTCPConnection(connConfig);

       

        try {

            connection.connect();

            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());

        } catch (Exception ex) {

            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());

            Log.e("XMPPClient", ex.toString());

            xmppClient.setConnection(null);

        }

        try

        {

            connection.login(username, password);

            Log.i("XMPPClient", "Logged in as " + connection.getUser());

 

            // Set the status to available

            Presence presence = new Presence(Presence.Type.available);

            connection.sendPacket(presence);

            xmppClient.setConnection(connection);

        }

        catch (Exception ex)

        {

            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);

            Log.e("XMPPClient", ex.toString());

                xmppClient.setConnection(null);

        }

        dismiss();

    }

   

    private String getText(int id) {

        EditText widget = (EditText) this.findViewById(id);

        return widget.getText().toString();

    }

   

    private void setText(int id,String text) {

        EditText widget = (EditText) this.findViewById(id);

        widget.setText(text);

    }

   

}

 

Current it is giving me could not determine smack version error.

Kindly Help

Thanks

Outcomes