适用于 Android 的 XMPP/facebook 聊天连接
Posted
技术标签:
【中文标题】适用于 Android 的 XMPP/facebook 聊天连接【英文标题】:XMPP/facebook chat connection for android 【发布时间】:2011-02-18 13:57:31 【问题描述】: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);
这段代码我用来与 facebook 聊天建立连接,但无法建立连接,我不知道为什么。它适用于 gmail
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)
【问题讨论】:
是否有任何日志消息告诉服务器响应是什么? 有时连接会在引发异常后打开,表示未连接到服务器,, 我更新了帖子并添加了错误消息 请参考此链接.. android 的 facebook 聊天连接有很好的答案,它对我有用。 ***.com/questions/11045241/… 【参考方案1】:变化:
private final static String LOGIN = "xxxxx@chat.facebook.com";
收件人:
private final static String LOGIN = "xxxxx";
不完全是;)。不过你明白了..
facebook 的 XMPP 规范不包括用于登录的 @server.here。要么将其包含在您的用户名字符串中,要么完全删除您的服务器字符串。
【讨论】:
【参考方案2】:Facebook 登录不同:
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);
【讨论】:
嗨 Raj..我如何实现 SaslClient..我对此一无所知...如果你在这方面请帮助我,因为我无法将 facebook 聊天与 XMPP 连接起来..跨度>以上是关于适用于 Android 的 XMPP/facebook 聊天连接的主要内容,如果未能解决你的问题,请参考以下文章
适用于 Android 的 Google Places API 未显示
Flutter 是不是适用于任何 Android 平台版本?