gpt4 book ai didi

java - 如何使用 xmpp 连接 Facebook 聊天,我想输入 friend 的用户名,然后聊天显示 SASL 身份验证失败

转载 作者:太空狗 更新时间:2023-10-29 14:15:25 26 4
gpt4 key购买 nike

我可以为 gtalk 连接 xmpp,但我不知道如何为 facebook 聊天连接 xmpp,我搜索了很多,然后我写了一些代码,它也不起作用,

现在我正在尝试这样,用户需要输入他的用户名和密码,然后用户必须输入他的 friend 的用户名和消息,然后聊天。

XMPPClient.java

public class XMPPClient extends Activity {

private ArrayList<String> messages = new ArrayList();
private Handler mHandler = new Handler();
private SettingsDialog mDialog;
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection connection;

/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i("XMPPClient", "onCreate called");
setContentView(R.layout.main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

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);
setListAdapter();

// Dialog for getting the xmpp settings
mDialog = new SettingsDialog(this);

// Set a listener to show the settings dialog
Button setup = (Button) this.findViewById(R.id.setup);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mHandler.post(new Runnable() {
public void run()
{
mDialog.show();
}
});
}
});

// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
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);
connection.sendPacket(msg);
messages.add(connection.getUser() + ":");
messages.add(text);
setListAdapter();
}
});
}

/**
* 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() {
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);
}
}

settings.java

     public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
private XMPPClient xmppClient;


public SettingsDialog(XMPPClient xmppClient) {
super(xmppClient);
this.xmppClient = xmppClient;
}

protected void onStart() {
super.onStart();
setContentView(R.layout.settings);
getWindow().setFlags(4, 4);
setTitle("XMPP Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}
public void onClick(View v) {
String host = getText(R.id.host);
String port = getText(R.id.port);
String service = getText(R.id.service);
String username = getText(R.id.userid);
String password = getText(R.id.password);

//GTalk...Host name : talk.google.com The port number is 5222 service name : gmail.com
//Yahoo...Host name : iopibm.msg.yahoo.com The default port is 5061 service name : yahoo.com
//Facebook Hostname : chat.facebook.com The port number is 5222 service = chat.facebook.com for authentication SASLAuthentication.supportSASLMechanism("PLAIN", 0);
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
XMPPConnection xmpp = new XMPPConnection(config);
try
{
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
xmpp.connect();
xmpp.login("268651109963113", "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY", "Application");
} catch (XMPPException e)
{
xmpp.disconnect();
e.printStackTrace();
}
}
private String getText(int id) {
EditText widget = (EditText) this.findViewById(id);
return widget.getText().toString();
}
}

SASLXFacebookPlatformMechanism.java

public class SASLXFacebookPlatformMechanism extends SASLMechanism {

private static final String NAME = "X-FACEBOOK-PLATFORM";

private String apiKey = "268651109963113";
private String access_token = "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY";

/**
* Constructor.
*/
public SASLXFacebookPlatformMechanism(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}

@Override
protected void authenticate() throws IOException, XMPPException {

getSASLAuthentication().send(new AuthMechanism(NAME, ""));
}

@Override
public void authenticate(String apiKey, String host, String acces_token)
throws IOException, XMPPException {
if (apiKey == null || acces_token == null) {
throw new IllegalArgumentException("Invalid parameters");
}

this.access_token = acces_token;
this.apiKey = apiKey;
this.hostname = host;

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

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

@Override
protected String getName() {
return NAME;
}

@Override
public void challengeReceived(String challenge) throws IOException {
byte[] response = null;

if (challenge != null) {
String decodedChallenge = new String(Base64.decode(challenge));
Map<String, String> parameters = getQueryMap(decodedChallenge);

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

long callId = new GregorianCalendar().getTimeInMillis();

String composedResponse = "api_key="
+ URLEncoder.encode(apiKey, "utf-8") + "&call_id=" + callId
+ "&method=" + URLEncoder.encode(method, "utf-8")
+ "&nonce=" + URLEncoder.encode(nonce, "utf-8")
+ "&access_token="
+ URLEncoder.encode(access_token, "utf-8") + "&v="
+ URLEncoder.encode(version, "utf-8");

response = composedResponse.getBytes("utf-8");
}

String authenticationText = "";

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

// Send the authentication to the server
getSASLAuthentication().send(new Response(authenticationText));
}

private Map<String, String> getQueryMap(String query) {
Map<String, String> map = new HashMap<String, String>();
String[] params = query.split("\\&");

for (String param : params) {
String[] fields = param.split("=", 2);
map.put(fields[0], (fields.length > 1 ? fields[1] : null));
}

return map;
}
}

现在我更新的代码在这些行中抛出 NPE

Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
connection.sendPacket(msg);

首先它会打开对话框,我们需要输入用户 ID 和密码,然后输入好友列表的用户名,然后是 msg,然后点击发送,我正在尝试,,,我正在输入用户名称也正确,但在单击发送按钮时显示 NUll ponter 异常

最佳答案

我认为问题可能是您在某个地方有 2 个包含相同包和类的 jar 文件。所以我强烈建议您再次检查所有 jar 文件并删除导致问题的所有文件。

从其中一个 JAR 文件中删除此包将解决问题。

关于java - 如何使用 xmpp 连接 Facebook 聊天,我想输入 friend 的用户名,然后聊天显示 SASL 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22660397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com