来自code.samsung.com的Android XMPP示例在connect上给出了KeyStoreException和NoSuchAlgorithmException

时间:2013-09-14 06:41:59

标签: android xmpp

我对XMPPConnection内容很新。我从code.samsung.com下载了一个虚拟代码并尝试运行该代码。但它LogCatjava.security.KeystoreException以及java.security.nosuchalgorithmexception

can't connect to mitesh@gmail.com中给予例外

我的代码是,

package com.demo.xmppchat;

import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
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.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class XMPPChatDemoActivity extends Activity {

public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
public static final String USERNAME = "mitesh@gmail.com";
public static final String PASSWORD = "******";

private XMPPConnection connection;
private ArrayList<String> messages = new ArrayList<String>();
private Handler mHandler = new Handler();

private EditText recipient;
private EditText textMessage;
private ListView listview;

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

    recipient = (EditText) this.findViewById(R.id.toET);
    textMessage = (EditText) this.findViewById(R.id.chatET);
    listview = (ListView) this.findViewById(R.id.listMessages);
    setListAdapter();

    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.sendBtn);
    send.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String to = recipient.getText().toString();
            String text = textMessage.getText().toString();

            Log.i("XMPPChatDemoActivity", "Sending text " + text + " to " + to);
            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);              
            if (connection != null) {
                connection.sendPacket(msg);
                messages.add(connection.getUser() + ":");
                messages.add(text);
                setListAdapter();
            }
        }
    });

    connect();
}

/**
 * Called by Settings dialog when a connection is establised with the XMPP
 * server
 * 
 * @param connection
 */
public void setConnection(XMPPConnection connection) {
    this.connection = 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() {
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Log.i("XMPPChatDemoActivity", "Text Recieved " + 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.listitem, messages);
    listview.setAdapter(adapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    try {
        if (connection != null)
            connection.disconnect();
    } catch (Exception e) {

    }
}

public void connect() {

    final ProgressDialog dialog = ProgressDialog.show(this,
            "Connecting...", "Please wait...", false);

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            XMPPConnection connection = new XMPPConnection(connConfig);

            try {
                connection.connect();
                Log.i("XMPPChatDemoActivity",
                        "Connected to " + connection.getHost());
            } catch (XMPPException ex) {
                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + connection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());
                setConnection(null);
            }
            try {
                // SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                connection.login(USERNAME, PASSWORD);
                Log.i("XMPPChatDemoActivity",
                        "Logged in as " + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                setConnection(connection);

                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {
                    Log.d("XMPPChatDemoActivity",
                            "--------------------------------------");
                    Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.d("XMPPChatDemoActivity",
                            "User: " + entry.getUser());
                    Log.d("XMPPChatDemoActivity",
                            "Name: " + entry.getName());
                    Log.d("XMPPChatDemoActivity",
                            "Status: " + entry.getStatus());
                    Log.d("XMPPChatDemoActivity",
                            "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());

                    Log.d("XMPPChatDemoActivity", "Presence Status: "
                            + entryPresence.getStatus());
                    Log.d("XMPPChatDemoActivity", "Presence Type: "
                            + entryPresence.getType());
                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : "
                            + entryPresence);

                }
            } catch (XMPPException ex) {
                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", ex.toString());
                setConnection(null);
            }

            dialog.dismiss();
        }
    });
    t.start();
    dialog.show();
}
}

代码有什么问题? 请帮助.. !!

3 个答案:

答案 0 :(得分:4)

我不知道为什么三星推荐过时的,不再维护的版本的aSmack。这种错误通常是由错误的TrustStore配置引起的。

我建议您尝试aSmack fork that is maintained.确保阅读自述文件并按照说明。否则你会遇到麻烦。

答案 1 :(得分:0)

这是一个代码片段,您可以使用它来避免密钥库异常和您描述的算法异常:

    XMPPConnection xmppCon;
    ConnectionConfiguration conf;            
    SASLAuthentication.registerSASLMechanism("DIGEST-MD5", SASLDigestMD5Mechanism.class);
    SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0);

    conf = new ConnectionConfiguration(HOST, PORT);//, SERVICE);
    conf.setDebuggerEnabled(true);
    conf.setSASLAuthenticationEnabled(true);
    conf.setCompressionEnabled(true);
    conf.setSecurityMode(SecurityMode.enabled);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
        conf.setTruststoreType("AndroidCAStore");
        conf.setTruststorePassword(null);
        conf.setTruststorePath(null);
    }else{
        conf.setTruststoreType("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"; 
        }
        conf.setTruststorePath(path);
    }

    xmppCon = new XMPPConnection(conf);

答案 2 :(得分:0)

你使用的是错误的asmack。使用buddycloud 2011。