Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之注册(二)
来源:互联网 发布:二战战斗机性能数据 编辑:程序博客网 时间:2024/05/17 20:27
/IM/src/com/example/imui/RegesterActivity.java
package com.example.imui;import org.jivesoftware.smack.PacketCollector;import org.jivesoftware.smack.SmackConfiguration;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.filter.AndFilter;import org.jivesoftware.smack.filter.PacketFilter;import org.jivesoftware.smack.filter.PacketIDFilter;import org.jivesoftware.smack.filter.PacketTypeFilter;import org.jivesoftware.smack.packet.IQ;import org.jivesoftware.smack.packet.Registration;import com.example.service.XxmpConnection;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class RegesterActivity extends Activity implements OnClickListener { private EditText account; private EditText password; private EditText nickname; private Button regester; private XMPPConnection connection; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: Toast.makeText(RegesterActivity.this, "连接服务器失败", 0).show(); break; case 1: Toast.makeText(RegesterActivity.this, "注册成功", 0).show(); break; case 2: Toast.makeText(RegesterActivity.this, "已有账号", 0).show(); break; case 3: Toast.makeText(RegesterActivity.this, "注册失败", 0).show(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.regester); account = (EditText) findViewById(R.id.reg_acount); password = (EditText) findViewById(R.id.reg_password); nickname = (EditText) findViewById(R.id.reg_nickname); regester = (Button) findViewById(R.id.bt_regester); regester.setOnClickListener(this); } @Override public void onClick(View v) { final String accountStr = account.getText().toString().trim(); final String passwordStr = password.getText().toString().trim(); new Thread(new Runnable() { @Override public void run() { connection = XxmpConnection.getConnection(); regester(accountStr, passwordStr); } }).start(); } private void regester(String accountStr, String passwordStr) { if (connection == null) { handler.sendEmptyMessage(0); } Registration registration = new Registration(); registration.setType(IQ.Type.SET); registration.setTo(connection.getServiceName()); Log.d("kkk", connection.getServiceName()); registration.setUsername(accountStr); registration.setPassword(passwordStr); registration.addAttribute("android", "geolo_createUser_android"); PacketFilter filter = new AndFilter(new PacketIDFilter( registration.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(registration); IQ resultIQ = (IQ) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); collector.cancel();// 停止请求results(是否成功的结果) if (resultIQ == null) { Log.e("RegistActivity", "No response from server."); handler.sendEmptyMessage(0); } else if (resultIQ.getType() == IQ.Type.RESULT) { handler.sendEmptyMessage(1); finish(); } else { if (resultIQ.getError().toString() .equalsIgnoreCase("conflict(409)")) { Log.e("RegistActivity", "IQ.Type.ERROR: " + resultIQ.getError().toString()); handler.sendEmptyMessage(2); } else { Log.e("RegistActivity", "IQ.Type.ERROR: " + resultIQ.getError().toString()); handler.sendEmptyMessage(3); } } }}
/IM/res/layout/regester.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户注册" android:layout_marginTop="20dp" android:textSize="@dimen/textsize11" android:layout_gravity="center_horizontal"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/acount" android:textSize="@dimen/textsize11" /> <EditText android:id="@+id/reg_acount" android:layout_width="@dimen/wigth" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:hint="@string/hint1" android:inputType="number" android:padding="2dp" android:textSize="@dimen/textsize9" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password" android:textSize="@dimen/textsize11" /> <EditText android:id="@+id/reg_password" android:layout_width="@dimen/wigth" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:hint="@string/hint2" android:inputType="textPassword" android:padding="2dp" android:textSize="@dimen/textsize9" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="昵称 : " android:textSize="@dimen/textsize11" /> <EditText android:id="@+id/reg_nickname" android:layout_width="@dimen/wigth" android:layout_height="wrap_content" android:background="@drawable/bg_text" android:padding="2dp" android:textSize="@dimen/textsize9" /> </LinearLayout> <Button android:layout_width="@dimen/wigth" android:layout_height="wrap_content" android:id="@+id/bt_regester" android:layout_marginTop="20dp" android:background="@drawable/button_circle" android:text="注册" android:layout_gravity="center_horizontal" android:textSize="@dimen/textsize9"/></LinearLayout>
0 0
- Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之注册(二)
- Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之登录(三)
- Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之环境搭建及与服务器建立连接(一)
- XMPP(二)-基于asmack+openfire的安卓客户端(仿QQ)的介绍以及个人心得
- XMPP(二)-基于asmack+openfire的安卓客户端(仿QQ)的介绍以及个人心得
- Android XMPP聊天的实现(asmack+openfire)
- 基于XMPP协议的Android IM研究(asmack)
- XMPP-Android基于openfire+asmack
- 基于XMPP的IM(openfire+spark)Spark 插件开发
- Android基于XMPP Smack Openfire开发IM【三】客户端接收服务器发送的消息
- Android基于XMPP Smack Openfire开发IM【三】客户端接收服务器发送的消息
- 基于asmack开发的Android开源IM客户端
- 基于Xmpp,Openfire和Asmack的聊天软件的开发(一)
- #征文再续#Android基于XMPP Smack Openfire开发IM即时聊天(二)
- Xmpp协议 OpenFire服务器 Asmack Android客户端 一些Bug的解决方法
- iOS 之基于XMPP的iphone聊天客户端(二)
- Iphone 上开发IM基于XMPP协议的LBS 程序(1),联通openfire服务器
- Android基于XMPP Smack Openfire开发IM(1)搭建openfire服务器
- Java的GC机制
- Mojito是一个JavaScript Web应用框架
- java面试之输入输出流
- 如何当一个好的面试官
- 单调dp 石子归并
- Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之注册(二)
- java中常用到得设计模式有哪几种?
- 浏览器 cookie 详解 -- 转载自网易博客gkecenter
- linux命令之cp:略过目录
- c++的一些小程序!
- spydroid源码分析(一):介绍spydroid每个包的大体功能
- sql行列互转,不是很专业,但也能满足需求。
- hdu_1003_Max Sum hdu_1058_Humble Numbers hdu_1059_Dividing
- Java操作Microsoft Office(Excel) Apache POI