Java 无法使用SASLXFacebookPlatformMechanism类制作android xmpp facebook聊天客户端

Java 无法使用SASLXFacebookPlatformMechanism类制作android xmpp facebook聊天客户端,java,android,facebook,xmpp,asmack,Java,Android,Facebook,Xmpp,Asmack,我正在尝试制作一个简单版本的facebook messenger。我使用的代码在gtalk中运行良好,需要使用facebook身份验证才能与facebook好友聊天。为此,我使用了SASLXFacebookPlatfromMechanism类,在该类中,我将存储api令牌和api密钥以及我应用程序的apisecret。问题是SASLXFacebookPlatfromMechanism.java类充满了我根本无法解决的错误- **MainActivity.java** public class

我正在尝试制作一个简单版本的facebook messenger。我使用的代码在gtalk中运行良好,需要使用facebook身份验证才能与facebook好友聊天。为此,我使用了SASLXFacebookPlatfromMechanism类,在该类中,我将存储api令牌和api密钥以及我应用程序的apisecret。问题是SASLXFacebookPlatfromMechanism.java类充满了我根本无法解决的错误-

**MainActivity.java**

public class MainActivity extends ActionBarActivity {

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.activity_main);

    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) {
           try
           {
               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();

           }catch(Exception e)
           {
               Toast.makeText(MainActivity.this, "Error="+e.getMessage(), Toast.LENGTH_LONG).show();
           }
        }
    });
}





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

SASLXFacebookPlatformMechanism.java

{

}

代码显示错误声明 类型SASLAuthentication中的方法sendString不适用于argumentsResponse。构造函数ResponseString未定义 在->getSASLAuthentication.sendnew ResponseauthenticationText行中

我还对如何在MainActivity.class中使用这个SASLXFacebookPlatformMechanism.java类感到困惑。我一直在尝试了解它是如何工作的,但失败了。如果能提供一份关于如何使用asmack库开发xmpp facebook聊天客户端的完整指南,我将不胜感激,因为互联网上缺少足够的相关文档。谢谢。 [我有apikey和apitoken,因此空字符串不会保持为空]

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

public SettingsDialog(MainActivity 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) {


    final String host = "chat.facebook.com";
    final String port = ""+5222;
    final String service = "chat.facebook.com";
    final String username = "*****";
    final String password = "****";

    new Thread() {
        public void run() {
            // Create a connection
            ConnectionConfiguration connConfig =new ConnectionConfiguration(host, Integer.parseInt(port), service);
            connConfig.setSASLAuthenticationEnabled(true);

            XMPPConnection connection = new XMPPConnection(connConfig);


            try {
                connection.connect();
                Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
            } catch (Exception ex) {
                Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
                Log.e("XMPPClient", ex.toString());
                xmppClient.setConnection(null);
            }

            try {
                connection.login(username, password);
                Log.i("XMPPClient", "Logged in as " + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                xmppClient.setConnection(connection);
            } catch (XMPPException ex) {
                Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
                Log.e("XMPPClient", ex.toString());
                    xmppClient.setConnection(null);
            }

        }
    }.start();


    dismiss();
}

private String getText(int id) {
    EditText widget = (EditText) this.findViewById(id);
    return widget.getText().toString();
}
public class SASLXFacebookPlatformMechanism extends SASLMechanism
private static final String NAME              = "X-FACEBOOK-PLATFORM";

private String              apiKey            = "";
private String              applicationSecret = "";
private String              sessionKey        = "";

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

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

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

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

    String[] keyArray = apiKeyAndSessionKey.split("\\|", 2);
    if (keyArray.length < 2)
    {
        throw new IllegalArgumentException(
                "API key or session key is not present");
    }

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

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

    String[] mechanisms = { "DIGEST-MD5" };
    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 = { "DIGEST-MD5" };
    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 sig =
                "api_key=" + apiKey + "call_id=" + callId + "method="
                        + method + "nonce=" + nonce + "session_key="
                        + sessionKey + "v=" + version + applicationSecret;

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

        String composedResponse =
                "api_key=" + URLEncoder.encode(apiKey, "utf-8")
                        + "&call_id=" + callId + "&method="
                        + URLEncoder.encode(method, "utf-8") + "&nonce="
                        + URLEncoder.encode(nonce, "utf-8")
                        + "&session_key="
                        + URLEncoder.encode(sessionKey, "utf-8") + "&v="
                        + URLEncoder.encode(version, "utf-8") + "&sig="
                        + URLEncoder.encode(sig, "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;
}

private String md5(String text) throws NoSuchAlgorithmException,
        UnsupportedEncodingException
{
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(text.getBytes("utf-8"), 0, text.length());
    return convertToHex(md.digest());
}

private String convertToHex(byte[] data)
{
    StringBuilder buf = new StringBuilder();
    int len = data.length;

    for (int i = 0; i < len; i++)
    {
        int halfByte = (data[i] >>> 4) & 0xF;
        int twoHalfs = 0;

        do
        {
            if (0 <= halfByte && halfByte <= 9)
            {
                buf.append((char) ('0' + halfByte));
            }
            else
            {
                buf.append((char) ('a' + halfByte - 10));
            }
            halfByte = data[i] & 0xF;
        } while (twoHalfs++ < 1);
    }

    return buf.toString();
}

@Override
protected String getAuthenticationText(String arg0, String arg1, String arg2) {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected String getChallengeResponse(byte[] arg0) {
    // TODO Auto-generated method stub
    return null;
}