0

嗨,我正在运行一个 Google 聊天应用程序,以下是代码

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collection;

import javax.net.ssl.SSLContext;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class FacebookChatActivity extends Activity implements OnClickListener {

private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection mConnection;
private String mHost, mPort, mService, mUsername, mPassword;
private ConnectionConfiguration mConnConfig;
private String TAG;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initLayout();
     initGtalk();
 //   initFB();
//
    // Create a connection
    createConnection();

    // login
    loginToXMPP();

}

void initLayout() {
    Log.i("XMPPClient", "onCreate called");
    setContentView(R.layout.activity_facebook_chat);

    mRecipient = (EditText) this.findViewById(R.id.recipient);
    Log.i("XMPPClient", "mRecipient = " + mRecipient);
    mSendText = (EditText) this.findViewById(R.id.sendText);
    Log.i("XMPPClient", "mSendText = " + mSendText);
    mList = (ListView) this.findViewById(R.id.listMessages);
    Log.i("XMPPClient", "mList = " + mList);
    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(this);

    setListAdapter();
}

void initGtalk() {
    mHost = "talk.google.com";
    mPort = "5222";
    mService = "gmail";
    mUsername = "userid@gmail.com";
    mPassword = "password";
    // Set Default recipients for Gtalk
    mRecipient.setText("rameshchoudury1990@gmail.com");
}

void initFB() {
    mHost = "chat.facebook.com";
    mPort = "5222";
    mService = "xmpp";
    mUsername = "userid@chat.facebook.com";
    mPassword = "password";
    // Set Default recipients for FB
    mRecipient.setText("new_userid@chat.facebook.com");
}

void createConnection() {
    mConnConfig = new ConnectionConfiguration(mHost,
            Integer.parseInt(mPort), mService);
    mConnConfig.setSecurityMode(SecurityMode.required);
    mConnConfig.setSASLAuthenticationEnabled(true);
    mConnection = new XMPPConnection(mConnConfig);

    try {
        mConnection.connect();
        Log.i("XMPPClient",
                "[SettingsDialog] Connected to " + mConnection.getHost());
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to "
                + mConnection.getHost());
        Log.e("XMPPClient", ex.toString());
        setConnection(null);
    }
}

void loginToXMPP() {
    try {
        mConnection.login(mUsername, mPassword);
        Log.i("XMPPClient", "Logged in as " + mConnection.getUser());

        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);
        mConnection.sendPacket(presence);
        setConnection(mConnection);
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "
                + mUsername);
        Log.e("XMPPClient", ex.toString());
        setConnection(null);
    }
}


/* * Called by Settings dialog when a connection is established with the XMPP
 * server
 * 
 * @param connection*/

public void setConnection(XMPPConnection connection) {
    this.mConnection = connection;
    if (connection != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody()
                            + "] from [" + fromName + "]");
                    messages.add(fromName + ":");
                    messages.add(message.getBody());
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                        }
                    });
                }
            }
        }, filter);
    }
}

private void setListAdapter() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item, messages);
    mList.setAdapter(adapter);
}

public void onClick(View v) {
    if (v.getId() == R.id.send) {
        String to = mRecipient.getText().toString();
        String text = mSendText.getText().toString();

        Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
        Message msg = new Message(to, Message.Type.chat);
        msg.setBody(text);
        mConnection.sendPacket(msg);
        messages.add(mConnection.getUser() + ":");
        messages.add(text);
        setListAdapter();
    }

}

}

当我运行此代码时,我得到

11-26 16:20:15.283: E/dalvikvm(595): Could not find class org.jivesoftware.smack.ConnectionConfiguration', referenced from method com.example.sarojfacebookchat.FacebookChatActivity.createConnection

谁能解释一下这个错误,如果需要,请给我建议正确的代码。我已将 asmack2010.05.07.jar 包含到我的项目中

4

4 回答 4

1

您会收到此错误的原因只有三个:

  1. 这个类真的不存在。如果您使用官方示例中的代码并获取此代码,请确保您拥有该库的最新版本
  2. 您尚未将 jar 添加到构建路径中。要解决此问题,请右键单击 Eclipse 中的 jar,然后执行 Build Path ► Add to Build Path。
  3. 您的 jar 不在/libs文件夹中。当您将 jar 添加到构建路径时会发生这种情况,但较新版本的 ADT 需要它位于/libs. 将其放在那里并将其重新添加到构建路径中。

大多数情况下,出现此类错误是因为较新版本的 ADT 要求所有外部 jar 都在/libs文件夹中。您的同事可能与您使用的版本不同,因此出现了错误。

于 2012-11-26T11:44:47.173 回答
1

你应该试试这个:

从 Java 构建路径中删除对项目中 JAR 的所有引用。-> 您的代码不应再无错误地构建

如果项目根目录中不存在 libs 文件夹,请创建一个 libs 文件夹 将 JAR 复制到 libs 文件夹中。

如果仍然没有运行确定。右键单击您的项目 > Android 工具 > 修复项目属性

清理你的项目并运行。它会起作用的

于 2012-11-26T11:21:04.763 回答
0

试试这个方法...

  1. 右键单击您的项目并转到属性。
  2. 转到java构建路径..//在左侧的第5个位置
  3. 转到订单和导出选项卡。
  4. 在您选择的 jar 文件上检查(勾选标记)。并单击确定。
  5. 现在,清理您的项目并运行。
于 2012-11-26T11:46:45.353 回答
0

刚刚也遇到了这个问题,........解决方案是从构建路径中删除smack jar并将jar文件复制并粘贴到项目的libs文件夹中。

于 2013-11-11T18:14:17.243 回答