简体   繁体   中英

Cannot connect to Openfire sever using xmpp connection configuration for Android Chat Application

can anyone help me? I'm new to android and currently trying to built an android chat application. However I have this issue that I am currently facing.

I am using Openfire as my server and while trying to connect to openfire, this error pops up and crashes the application when I clicked the button to login.

This is my LoginActivity.java

package com.example.chattp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

private EditText accountEditText;
private EditText passwordEditText;
protected Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login);
    accountEditText = (EditText) findViewById(R.id.login_account);
    passwordEditText = (EditText) findViewById(R.id.login_password);


    findViewById(R.id.login_login).setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            String account = accountEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            if(account.equals("") || password.equals(""))
            {
                Toast.makeText(LoginActivity.this, "Username and Password must be entered", Toast.LENGTH_SHORT).show();
            }else
            {
                ClientConServer ccs = new ClientConServer (LoginActivity.this);
                boolean b = ccs.login(account, password);

                //if successful login
                if(b) 
                {
                    Toast.makeText(LoginActivity.this, "Login is Successful", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginActivity.this, TestActivity.class));

                }else{
                    Toast.makeText(LoginActivity.this, "Login is unsuccessful", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}


public class ClientConServer {
    private static final String HOST = "192.168.1.4";
    private static final int PORT = 5222;
    private Context context;
    public ClientConServer(Context context){
        this.context = context;
    }
    public boolean login(String a, String p){
        ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);
        config.setSASLAuthenticationEnabled(false);  

        XMPPConnection connection = new XMPPConnection(config); 
        try{
            connection.connect();
            connection.login(a, p);
            return true;
        } catch(XMPPException e){
            e.printStackTrace();
        }
        return false;

    }
}

Errors shown on Logcat

01-14 23:35:35.500: E/dalvikvm(2533): Could not find class 'org.jivesoftware.smack.ConnectionConfiguration', referenced from method com.example.chattp.LoginActivity$ClientConServer.login
01-14 23:35:35.500: W/dalvikvm(2533): VFY: unable to resolve new-instance 823 (Lorg/jivesoftware/smack/ConnectionConfiguration;) in Lcom/example/chattp/LoginActivity$ClientConServer;
01-14 23:35:35.510: D/dalvikvm(2533): VFY: replacing opcode 0x22 at 0x0001
01-14 23:35:35.510: D/dalvikvm(2533): DexOpt: unable to opt direct call 0x14d5 at 0x07 in Lcom/example/chattp/LoginActivity$ClientConServer;.login
01-14 23:35:35.580: D/dalvikvm(2533): DexOpt: unable to opt direct call 0x14d7 at 0x0f in Lcom/example/chattp/LoginActivity$ClientConServer;.login
01-14 23:35:35.590: D/AndroidRuntime(2533): Shutting down VM
01-14 23:35:35.590: W/dalvikvm(2533): threadid=1: thread exiting with uncaught exception (group=0xb0eb8648)
01-14 23:35:35.610: E/AndroidRuntime(2533): FATAL EXCEPTION: main
01-14 23:35:35.610: E/AndroidRuntime(2533): java.lang.NoClassDefFoundError: org.jivesoftware.smack.ConnectionConfiguration
01-14 23:35:35.610: E/AndroidRuntime(2533):     at com.example.chattp.LoginActivity$ClientConServer.login(LoginActivity.java:70)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at com.example.chattp.LoginActivity$1.onClick(LoginActivity.java:45)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.view.View.performClick(View.java:4240)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.view.View$PerformClick.run(View.java:17721)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.os.Handler.handleCallback(Handler.java:730)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.os.Looper.loop(Looper.java:137)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at java.lang.reflect.Method.invoke(Method.java:525)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 23:35:35.610: E/AndroidRuntime(2533):     at dalvik.system.NativeStart.main(Native Method)

Usually these java.lang.NoClassDefFoundError: org.jivesoftware.smack.ConnectionConfiguration type of errors will come when the app is not recoginzed the jar files . in this case it's smack.jar .

Even I faced this issue i have selected the smack.jar in project- > properties - > Order and Export and make sure to include in Libraries.And do clean and re-build the project .Sometimes restart of eclipse also works.

public class Connection {

public final static String SERVER_HOST = "192.168.88.158";
public final static int SERVER_PORT = 5222;
public final static String SERVICE_NAME = "192.168.88.158";
public final static String LOGIN = "manish";
public final static String PASSWORD = "android";

private Context context;
static XMPPConnection m_connection;
static Presence presence;
public static ArrayList<HashMap<String, String>> usersList;

public static ConnectionConfiguration config = new ConnectionConfiguration(
        SERVER_HOST, SERVER_PORT, SERVICE_NAME);

public Connection(Context context) {
    // TODO Auto-generated constructor stub
    this.context = context;
}
public static void initConnection() throws XMPPException {
    // Initialization de la connexion
    m_connection = new XMPPConnection(config);
    m_connection.connect();

            //Create user
     AccountManager newAccount = new AccountManager(m_connection);
     m_connection = new XMPPConnection(config);
     m_connection.connect();
     newAccount.createAccount("Rahul", "android");
     presence = new Presence(Presence.Type.available);
     m_connection.sendPacket(presence); }}

public boolean login(String username, String Password){
    m_connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(false);
    XMPPConnection connection = new XMPPConnection(config); `enter code here`
    try{
        connection.connect();
        connection.login(username, Password);
        return true;`enter code here`
    } catch(XMPPException e){`enter code here`
        e.printStackTrace();
    }
    return false; }}

for establishing a connection it's really help's you . And any query related to registry user in openfire server always welcome. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM