简体   繁体   English

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

[英]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. 我是android新手,目前正在尝试构建android聊天应用程序。 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. 我将Openfire用作服务器,并尝试连接到Openfire时,单击该按钮登录时,会弹出此错误并使应用程序崩溃。

This is my LoginActivity.java 这是我的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 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 . 通常,当应用程序未重新识别jar文件时,会出现以下java.lang.NoClassDefFoundError:org.jivesoftware.smack.ConnectionConfiguration类型的错误。 in this case it's smack.jar . 在这种情况下,它是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. 即使我也遇到了这个问题,我也选择了project-> properties-> Order and Export中的smack.jar并确保将其包含在Libraries.And中进行清理并重新构建项目。

public class Connection { 公共类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. 并且总是欢迎与openfire服务器中的注册表用户相关的任何查询。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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