如何连接Facebook聊天使用XMPP,我要进入的朋友的用户名,然后聊天显示SASL验证失败 [英] How to connect Facebook chat using xmpp,i want to enter username of friends and then chat-showing SASL Authentication failed

查看:152
本文介绍了如何连接Facebook聊天使用XMPP,我要进入的朋友的用户名,然后聊天显示SASL验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以连接XMPP的,每增加一个,但我不知道如何connnect XMPP为Facebook聊天,我搜索很多,然后我写了一些code,它不是工作压力太大,

I am able to connect xmpp for gtalk,but i dont know how to connnect xmpp for facebook chat,i searched lot,and then i wrote some code,its not working too,

现在我试图这样,用户需要输入用户标识自己和pwd,然后用户必须键入他​​的朋友们用户名和味精,然后聊天。

Now i am trying like this,user needs to type his userid and pwd and then user has to type his friends username and msg and then chat.

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;
}
}

现在我更新$ C $这些行C投掷NPE

Now my updated code throwing NPE in these lines

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

首先,它会打开对话框,从那里,我们需要输入用户ID和PWD,然后要输入的好友列表中的用户名,然后味精,然后点击发送这样我试图,,,我打字的用户名称是否正确太多,但其表现为空ponter例外,同时点击发送按钮

First it will open the dialog box from there we need to type the userid and pwd and then wants to type the username of friends list and then msg and then click send like this i am trying,,,i am typing the user name correctly too,but its showing NUll ponter exception while clicking send button

推荐答案

我想问题可能是你有2个jar文件的地方,包括相同的包和类。所以,我强烈建议你检查你的所有jar文件的再次并删除任何人,这是造成这个问题。

I think the problem might be you have 2 jar files somewhere that include the same package and classes. So i strongly recommend you to check your all jar file's once again and delete anyone which is causing the problem.

删除这个包从JAR文件中的一个将问题解决了。

Deleting this package from one of the JAR files will solved the problem.

这篇关于如何连接Facebook聊天使用XMPP,我要进入的朋友的用户名,然后聊天显示SASL验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆