手机上使用asmack开发xmpp客户端

openfire服务端,smack:
     下载地址:http://www.igniterealtime.org/downloads/index.jsp
     源代码:http://www.igniterealtime.org/downloads/source.jsp
android客户端库,asmack:
    首页:https://github.com/Flowdalic/asmack
    源代码及jar包:http://asmack.freakempire.de/
服务端搭建指导:http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html
客户端样例:http://www.cnblogs.com/hoojo/archive/2012/06/25/2561576.html

自己编写的样例应用:http://files.cnblogs.com/jerry1999/xmppClient.rar

主要代码:

package com.example.xmppTest;



import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.MessageListener;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Packet;



import android.util.Log;



public class XMPPTester {

	private XMPPConnection connection;

	private InteractCallback interactCallback;



	public XMPPTester(InteractCallback paramInteractCallback) {

		this.interactCallback = paramInteractCallback;

	}



	private void printLog(String paramString) {

		System.out.println(paramString);

		Log.v("XMPPTester", paramString);

	}



	public void connectXMPP(String paramString1, int paramInt,

			String paramString2, String paramString3, String paramString4)

			throws XMPPException {

		ConnectionConfiguration localConnectionConfiguration = new ConnectionConfiguration(

				paramString1, paramInt);

		localConnectionConfiguration.setDebuggerEnabled(true);

		localConnectionConfiguration.setSASLAuthenticationEnabled(true);

		localConnectionConfiguration

				.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);

		localConnectionConfiguration.setServiceName(paramString4);

		localConnectionConfiguration.setCompressionEnabled(true);

		localConnectionConfiguration.setReconnectionAllowed(true);

		this.connection = new XMPPConnection(localConnectionConfiguration);

		this.connection.connect();

		loginUser(paramString2, paramString3);

	}



	public void loginUser(String userName, String password)

			throws XMPPException {

		this.connection.login(userName, password);

		this.connection.addPacketListener(new PacketListener() {

			public void processPacket(Packet paramAnonymousPacket) {

				String str = paramAnonymousPacket.toXML();

				XMPPTester.this.interactCallback.onReceived(str);

			}

		}, new PacketFilter() {

			public boolean accept(Packet paramAnonymousPacket) {

				XMPPTester.this.printLog("PacketFilter.accept");

				return true;

			}

		});

	}



	public void sendMessage(String paramString1, String paramString2)

			throws XMPPException {

		Chat localChat = this.connection.getChatManager().createChat(

				paramString1, new MyMessageListeners());

		Message localMessage = new Message();

		localMessage.setBody(paramString2);

		localChat.sendMessage(localMessage);

	}



	class MyMessageListeners implements MessageListener {

		MyMessageListeners() {

		}



		public void processMessage(Chat paramChat, Message paramMessage) {

		}

	}

}

 

package com.example.xmppTest;



public interface InteractCallback {

      public abstract void onConnected();

      

      public abstract void onException(Exception paramException);

      

      public abstract void onReceived(String paramString);

      

      public abstract void onSentSuccess();

}
View Code
package com.example.xmppTest;



import org.jivesoftware.smack.XMPPException;



import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;



public class MainActivity extends Activity implements InteractCallback,

        View.OnClickListener {

    Button connectButton;

    private String host;

    private EditText hostEditText;

    InteractHandler interactHandler = new InteractHandler();

    private String message;

    private EditText messageEditText;

    private String name;

    private EditText nameEditText;

    private EditText passEditText;

    private String password;

    private int port;

    private EditText portEditText;

    Button sendButton;

    private EditText serviceEditText;

    private String serviceName;

    private String to;

    private EditText toEditText;

    private XMPPTester xMPPTester = new XMPPTester(this);



    private void initFieldVars() {

        this.connectButton = ((Button) findViewById(R.id.connectButton));

        this.sendButton = ((Button) findViewById(R.id.sendButton));

        this.hostEditText = ((EditText) findViewById(R.id.hostEditText1));

        this.portEditText = ((EditText) findViewById(R.id.portEditText));

        this.serviceEditText = ((EditText) findViewById(R.id.serviceEditText));

        this.nameEditText = ((EditText) findViewById(R.id.nameEditText));

        this.passEditText = ((EditText) findViewById(R.id.passEditText));

        this.toEditText = ((EditText) findViewById(R.id.toEditText));

        this.messageEditText = ((EditText) findViewById(R.id.messageEditText));

    }



    private void initInputFields() {

        this.host = this.hostEditText.getText().toString();

        this.port = Integer.parseInt(this.portEditText.getText().toString());

        this.serviceName = this.serviceEditText.getText().toString();

        this.name = this.nameEditText.getText().toString();

        this.password = this.passEditText.getText().toString();

        this.to = (this.toEditText.getText().toString() + "@" + this.serviceName);

        this.message = this.messageEditText.getText().toString();

    }



    @SuppressWarnings("unchecked")

    private void onConnectButtonClick() {

        initInputFields();

        new AsyncTask() {

            protected Object doInBackground(Object... paramAnonymousVarArgs) {

                try {

                    MainActivity.this.xMPPTester.connectXMPP(

                            MainActivity.this.host, MainActivity.this.port,

                            MainActivity.this.name, MainActivity.this.password,

                            MainActivity.this.serviceName);

                    Log.v("XMPPTester", "connected." + MainActivity.this.name);

                    MainActivity.this.onConnected();

                    return null;

                } catch (XMPPException localXMPPException) {

                    for (;;) {

                        MainActivity.this.onException(localXMPPException);

                        localXMPPException.printStackTrace();

                        Log.v("XMPPTester",

                                "error: " + localXMPPException.getMessage());

                    }

                }

            }

        }.execute(new Object[0]);

    }



    private void onSendButtonClick() {

        initInputFields();

        new AsyncTask() {

            protected Object doInBackground(Object... paramAnonymousVarArgs) {

                try {

                    MainActivity.this.xMPPTester.sendMessage(

                            MainActivity.this.to, MainActivity.this.message);

                    Log.v("XMPPTester", "Sent ." + MainActivity.this.name);

                    MainActivity.this.onSentSuccess();

                    return null;

                } catch (XMPPException localXMPPException) {

                    for (;;) {

                        MainActivity.this.onException(localXMPPException);

                        localXMPPException.printStackTrace();

                        Log.v("XMPPTester",

                                "error: " + localXMPPException.getMessage());

                    }

                }

            }

        }.execute(new Object[0]);

    }



    public void onClick(View paramView) {

        switch (paramView.getId()) {

        case R.id.connectButton:

            onConnectButtonClick();

            return;

        case R.id.sendButton:

            onSendButtonClick();

            return;

        }

    }



    public void onConnected() {

        Message localMessage = new Message();

        Bundle localBundle = new Bundle();

        localBundle.putString("result", "Connect success");

        localMessage.setData(localBundle);

        this.interactHandler.sendMessage(localMessage);

    }



    protected void onCreate(Bundle paramBundle) {

        super.onCreate(paramBundle);

        setContentView(R.layout.main);

        initFieldVars();

        this.connectButton.setOnClickListener(this);

        this.sendButton.setOnClickListener(this);

    }



    public void onException(Exception paramException) {

        Message localMessage = new Message();

        Bundle localBundle = new Bundle();

        localBundle

                .putString("result", "Error: " + paramException.getMessage());

        localMessage.setData(localBundle);

        this.interactHandler.sendMessage(localMessage);

    }



    public void onReceived(String paramString) {

        Message localMessage = new Message();

        Bundle localBundle = new Bundle();

        localBundle.putString("result", "Received: " + paramString);

        localMessage.setData(localBundle);

        this.interactHandler.sendMessage(localMessage);

    }



    public void onSentSuccess() {

        Message localMessage = new Message();

        Bundle localBundle = new Bundle();

        localBundle.putString("result", "Sent success");

        localMessage.setData(localBundle);

        this.interactHandler.sendMessage(localMessage);

    }



    class InteractHandler extends Handler {

        public void handleMessage(Message paramMessage) {

            super.handleMessage(paramMessage);

            String str = paramMessage.getData().get("result").toString();

            Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();

        }

    }

}
View Code

 

你可能感兴趣的:(smack)