无法使用 Android 聊天应用程序的 xmpp 连接配置连接到 Openfire 服务器



谁能帮我?我是安卓新手,目前正在尝试构建一个安卓聊天应用程序。但是我目前面临这个问题。

我正在使用 Openfire 作为我的服务器,在尝试连接到 openfire 时,当我单击按钮登录时,会弹出此错误并使应用程序崩溃。

这是我的登录活动.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;
    }
}

日志猫上显示的错误

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)

通常这些java.lang.NoClassDefFoundError:org.jivesoftware.smack.Connection配置类型的错误会在应用程序未识别jar文件时出现。 在这种情况下,它很啪啪.jar .

即使我遇到了这个问题,我也选择了项目>属性中的.jar>订购和导出,并确保包含在库中,并清理并重建项目。有时重新启动日食也有效。

公共类连接 {

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; }}

为了建立连接,它真的有帮助你。并且始终欢迎与openfire服务器中的注册表用户相关的任何查询。谢谢

最新更新