English 中文(简体)
XMPP/facebook chat connection for android
原标题:XMPP/facebook chat connection for android
public class ClientJabberActivity extends Activity {

private final static String SERVER_HOST = "chat.facebook.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "chat.facebook.com"; 
private final static String LOGIN = "xxxxx@chat.facebook.com";
private final static String PASSWORD = "xxxxxx";


private List<String> m_discussionThread;
private ArrayAdapter<String> m_discussionThreadAdapter;
private XMPPConnection m_connection;
private Handler m_handler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    m_handler = new Handler();
    try {
        initConnection();
    } catch (XMPPException e) {
        e.printStackTrace();
    }

    final EditText recipient = (EditText) this.findViewById(R.id.recipient);
    final EditText message = (EditText) this.findViewById(R.id.message);        
    ListView list = (ListView) this.findViewById(R.id.thread);

    m_discussionThread = new ArrayList<String>();
    m_discussionThreadAdapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item, m_discussionThread);
    list.setAdapter(m_discussionThreadAdapter);

    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String to = recipient.getText().toString();
            String text = message.getText().toString();

            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            m_connection.sendPacket(msg);
            m_discussionThread.add("moi :");
            m_discussionThread.add(text);
            m_discussionThreadAdapter.notifyDataSetChanged();
        }
    });
}


private void initConnection() throws XMPPException {
    //Initialisation de la connexion
    ConnectionConfiguration config =
            new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
    m_connection = new XMPPConnection(config);
    m_connection.connect();
    m_connection.login(LOGIN, PASSWORD);
    Presence presence = new Presence(Presence.Type.available);
    m_connection.sendPacket(presence);

    //enregistrement de l écouteur de messages
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    m_connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    m_discussionThread.add(fromName + ":");
                    m_discussionThread.add(message.getBody());

                    m_handler.post(new Runnable() {
                        public void run() {
                            m_discussionThreadAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }
        }, filter);
}

}

这部法典用于与面貌相连接,但不能建立联系,不知道为什么。 它很好地利用了电子邮件。

02-18 16:06:20.178: ERROR/AndroidRuntime(453): java.lang.IllegalStateException: Not connected to server.
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:665)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at com.developpez.florentgarin.android.ClientJabberActivity$1.onClick(ClientJabberActivity.java:61)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.view.View.performClick(View.java:2485)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.view.View$PerformClick.run(View.java:9080)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.os.Looper.loop(Looper.java:123)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at android.app.ActivityThread.main(ActivityThread.java:3647)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at java.lang.reflect.Method.invoke(Method.java:507)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 16:06:20.178: ERROR/AndroidRuntime(453):     at dalvik.system.NativeStart.main(Native Method)
02-18 16:06:43.167: ERROR/installd(35): eof
02-18 16:06:43.167: ERROR/installd(35): failed to read size
02-18 16:07:11.857: ERROR/Zygote(470): setreuid() failed. errno: 17
02-18 16:07:16.408: ERROR/BatteryService(477): usbOnlinePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryVoltagePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryTemperaturePath not found
02-18 16:07:16.477: ERROR/SurfaceFlinger(477): Couldn t open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
02-18 16:07:16.758: ERROR/SensorService(477): couldn t open device for module sensors (Invalid argument)
问题回答

变化:

private final static String LOGIN = "xxxxx@chat.facebook.com";

:

private final static String LOGIN = "xxxxx";

确实不是; 你们可以这样说。

XMPP 脸谱不包括“@server”。 页: 1 两者都包含在您的用户名中,显示或删除完全显示的服务器。

书目不同:

package com.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.Base64;

import de.measite.smack.Sasl;

public class FacebookConnectSASLMechanism  extends SASLMechanism
{

    private String sessionKey = "";
    private String sessionSecret = "";
    private String apiKey = "";

    public FacebookConnectSASLMechanism(SASLAuthentication saslAuthentication) {
            super(saslAuthentication);
    }

    // protected void authenticate() throws IOException, XMPPException {
    // String[] mechanisms = { getName() };
    // Map<String, String> props = new HashMap<String, String>();
    // sc = Sasl.createSaslClient(mechanisms, null, "xmpp", hostname, props,
    // this);
    //
    // super.authenticate();
    // }

    protected void authenticate() throws IOException, XMPPException {
            StringBuilder stanza = new StringBuilder();
            stanza.append("<auth mechanism="").append(getName());
            stanza.append("" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">");
            stanza.append("</auth>");

            // Send the authentication to the server
            //getSASLAuthentication().send(stanza.toString());
    }

    public void authenticate(String apiKeyAndSessionKey, String host, String sessionSecret)
                    throws IOException, XMPPException {

            if(apiKeyAndSessionKey==null || sessionSecret==null)
                    throw new IllegalStateException("Invalid parameters!");

            String[] keyArray = apiKeyAndSessionKey.split("\|");

            if(keyArray==null || keyArray.length != 2)
                    throw new IllegalStateException("Api key or session key is not present!");

            this.apiKey = keyArray[0];
            this.sessionKey = keyArray[1];
            this.sessionSecret = sessionSecret;

            this.authenticationId = sessionKey;
            this.password = sessionSecret;
            this.hostname = host;

            String[] mechanisms = { "DIGEST-MD5" };
            Map<String, String> props = new HashMap<String, String>();
            sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
            authenticate();
    }

    public void authenticate(String username, String host, CallbackHandler cbh)
                    throws IOException, XMPPException {
            String[] mechanisms = { "DIGEST-MD5" };
            Map<String, String> props = new HashMap<String, String>();
            sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
            authenticate();
    }

    protected String getName() {
            return "X-FACEBOOK-PLATFORM";
    }

    public void challengeReceived(String challenge) throws IOException {
            // Build the challenge response stanza encoding the response text
            StringBuilder stanza = new StringBuilder();

            byte response[] = null;
            if (challenge != null) {
                    String decodedResponse = new String(Base64.decode(challenge));
                    Map<String, String> parameters = getQueryMap(decodedResponse);

                    String version = "1.0";
                    String nonce = parameters.get("nonce");
                    String method = parameters.get("method");

                    Long callId = new GregorianCalendar().getTimeInMillis()/1000;

                    String sig = "api_key="+apiKey
                                                    +"call_id="+callId
                                                    +"method="+method
                                                    +"nonce="+nonce
                                                    +"session_key="+sessionKey
                                                    +"v="+version
                                                    +sessionSecret;

                    try {
                            sig = MD5(sig);
                    } catch (NoSuchAlgorithmException e) {
                            throw new IllegalStateException(e);
                    }

                    String composedResponse = "api_key="+apiKey+"&"
                                                                            +"call_id="+callId+"&"
                                                                            +"method="+method+"&"
                                                                            +"nonce="+nonce+"&"
                                                                            +"session_key="+sessionKey+"&"
                                                                            +"v="+version+"&"
                                                                            +"sig="+sig;

                    response = composedResponse.getBytes();
            }

            String authenticationText="";

            if (response != null) {
                    authenticationText = Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);
            }

            stanza.append("<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">");
            stanza.append(authenticationText);
            stanza.append("</response>");

            // Send the authentication to the server
            //getSASLAuthentication().send(stanza);
    }

    private Map<String, String> getQueryMap(String query) {
            String[] params = query.split("&");
            Map<String, String> map = new HashMap<String, String>();
            for (String param : params) {
                    String name = param.split("=")[0];
                    String value = param.split("=")[1];
                    map.put(name, value);
            }
            return map;
    }

private String convertToHex(byte[] data) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do {
            if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ( 0  + halfbyte));
            else
                buf.append((char) ( a  + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    }
    return buf.toString();
}

public String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  {
    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();
    return convertToHex(md5hash);
}
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签