手机上使用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();
}
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();
}
}
}
手机上使用asmack开发xmpp客户端的更多相关文章
- 使用termux在安卓手机上搭建python开发环境
下载安装termux应用. 应用链接如下:https://download.csdn.net/upload/11342944设置-应用-termux赋予存储权限 cd mkdir .termux vi ...
- WPF一步步开发XMPP IM客户端1:入门
[起因&目标] 因为工作原因接触openfire服务端和spark客户端开发,主要是基于openfire扩展开发了针对企业用途的服务器插件,还开发了各个平台上的客户端(Windows\mac\ ...
- XMPP客户端开发(1)--连接和登录
Smack可用于XMPP客户端的开发,下载Smack,将相关jar文件导入后,即可以开始XMPP客户端的开发. 以下代码实现了客户端连接Tigase服务器,并根据用户名和密码登录. package X ...
- ios 给微信开发一个插件并安装到未越狱的手机上教程
现来整体说一下思路,首先给越狱的手机开发一个插件并安装上去,然后去越狱手机上找到相应的动态库和对应的微信APP安装包,拷贝出来,然后重新签名,就可以安装到未越狱的手机上了 1.首先你的电脑需要安装th ...
- android的开发 华为手机上不显示menu键
android的开发,华为手机上不显示menu键解决办法: 在AndroidManifest.xml中讲targetSdkVersion改为9. <uses-sdk android:minSdk ...
- 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...
- 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接(转)
前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...
- HDD线上沙龙·创新开发专场:多元服务融合,助力应用创新开发
5月24日,由华为开发者联盟主办的HUAWEI Developer Day(华为开发者日,简称HDD)线上沙龙·创新开发专场在华为开发者学堂及各大直播平台与广大开发者见面.直播内容主要聚焦Harmon ...
- 关于如何在github上创建团队开发环境
今天想写个如何在github上创建团队开发环境的博客.送给那些还不知道如何在github上创建团队开发环境的开发人员. 1.首先,当然你要有个github的账号.具体怎么注册我这里就不说了.可以上gi ...
随机推荐
- SQL给查询结果加序号
情境:在用delphi7编程时,想要给查询出的结果一个编号,比方有一万条结果,就自己主动从1编号到10000 显示数据时用的是DBGrid控件,可是它的第一列无法非常好的显示编号,找了非常多方法都不能 ...
- Android布局属性详解剖析
View的布局显示方式有下面几种: 线性布局(LinearLayout) 相对布局(RelativeLayout) 表格布局(TableLayout) 网格视图(GridView) 标签布局(TabL ...
- 【转】IOS7 MPMoviePlayerViewController横屏显示
在应用程序中用到MPMoviePlayerViewController时,有时需要保持应用程序为竖屏状态,而视频播放器显示为横屏,如何做呢?如果采用强制横屏的方法,应用审核的时候是不会通过的,因为该方 ...
- MM32 RTC学习(兼容STM32)
RTC学习 RTC简述 实时时钟是一个独立的定时器. RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能. 修改计数器的值可以重新设置系统当前的时间和日期. RTC模块和时钟配 ...
- axure RP Pro7.0加载日历控件的步骤
- spring源码分析构建
命令如下: ant ant install-maven ant jar package E:\download\spring-framework-3.1.3.RELEASE\build-spring- ...
- errno.h 错误码描述.
描述:一般说的Linux源码的目录,默认是基于 /usr/include/ 的. 使用 char *strerror(int errnum); 函数打印错误代码的描述.我简单对比了一下,发现描述大体一 ...
- react-native-router-flux 下部导航
github url:https://github.com/aksonov/react-native-router-flux API: https://github.com/aksonov/react ...
- Java反射 - 2(对象复制,父类域,内省)
为什么要复制对象?假设有个类Car,包含name,color2个属性,那么将car1对象复制给car2对象,只需要car2.setName(car1.getName)与car2.setColor(ca ...
- maven 整理
1. 打包命令: mvn package -DskipTests 2. 发布命令: mvn deploy -DperformRelease=true