六:基于XMPP的smack注册

     吐舌头 接着上一篇,一个软件不能只从服务器端手动添加用户,那么这个软件刚到用户手里是不能用,所有今天写注册。

      start:

1、注册界面比较简单,因为目的是为了写聊天,其他的就简单写了。


activity_regist.xml,注册界面的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_regist_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="106dp"
        android:text="@string/regist_account"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_regist_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_regist_username"
        android:layout_toRightOf="@+id/tv_regist_rUsername"
        android:ems="10"
        android:hint="@string/et_regist_account" />

    <TextView
        android:id="@+id/tv_regist_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_regist_username"
        android:layout_below="@+id/tv_regist_username"
        android:layout_marginTop="20dp"
        android:text="@string/regist_password"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_regist_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_regist_password"
        android:layout_toRightOf="@+id/tv_regist_rUsername"
        android:ems="10"
        android:hint="@string/et_regist_password"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tv_regist_rUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_regist_password"
        android:layout_below="@+id/tv_regist_password"
        android:layout_marginTop="16dp"
        android:text="@string/regist_rPassword"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_regist_rPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_regist_rUsername"
        android:layout_alignBottom="@+id/tv_regist_rUsername"
        android:layout_toRightOf="@+id/tv_regist_rUsername"
        android:ems="10"
        android:hint="@string/et_regist_rPassword"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_regist_sure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_regist_password"
        android:layout_marginTop="88dp"
        android:text="@string/regist_sure" />

</RelativeLayout>
2、把下面的代码加到values下的string.xml里,代码:

 <!-- 注册 -->
    <string name="regist_account">用户名:</string>
    <string name="et_regist_account">请输入用户名</string>
    <string name="regist_password">密		码:</string>
    <string name="et_regist_password">请输入密码</string>
    <string name="regist_rPassword">确认密码:</string>
    <string name="et_regist_rPassword">请重新输入密码</string>
    <string name="regist_sure">确定</string>
    <string name="passwordNoUnify">密码不统一</string>
    <string name="regist_fail">注册失败</string>
    <string name="regist_success">注册成功</string>
    <string name="ServiceNoResponse">服务器没有响应</string>
    <string name="usernameIsExist">用户已存在</string>
3、注册的java代码,挺简单的,几个控件,点击一下,没什么要说,RegistActivity.java

package org.hkby.lwx.activity;

import org.hkby.lwx.common.Constant;
import org.hkby.lwx.task.RegistTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * register
 * 
 * @author liaowuxing
 * 
 */
public class RegistActivity extends Activity {

	private EditText et_username, et_password, et_rPassword;
	private Button btn_sure;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Remove the title bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_regist);
		initView();
		initClick();
	}

	public void initView() {
		et_username = (EditText) this.findViewById(R.id.et_regist_username);
		et_password = (EditText) this.findViewById(R.id.et_regist_password);
		et_rPassword = (EditText) this.findViewById(R.id.et_regist_rPassword);
		btn_sure = (Button) this.findViewById(R.id.btn_regist_sure);
	}

	public void initClick() {
		btn_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String username = et_username.getText().toString();
				String password = et_password.getText().toString();
				String rPassword = et_rPassword.getText().toString();
				if (username == "" || password == "" || username.length() == 0
						|| password.length() == 0) {
					Toast.makeText(RegistActivity.this, R.string.isEmpty,
							Toast.LENGTH_SHORT).show();
				} else if (password.length() != rPassword.length()
						|| !password.equals(rPassword)) {
					Toast.makeText(RegistActivity.this,
							R.string.passwordNoUnify, Toast.LENGTH_SHORT)
							.show();
				} else {
					RegistTask registTask = new RegistTask(RegistActivity.this);
					String isSuccess = registTask
							.getExecute(username, password);
					if (isSuccess == Constant.regist_success) {
						et_username.setText("");
						et_password.setText("");
						et_rPassword.setText("");
					} else {
						et_password.setText("");
						et_rPassword.setText("");
					}
				}
			}
		});
	}
}
4、注册需要开一个线程,我用异步任务,创建一个类:RegistTask.java

<pre name="code" class="java">package org.hkby.lwx.task;

import java.util.concurrent.ExecutionException;

import org.hkby.lwx.activity.R;
import org.hkby.lwx.common.Constant;
import org.hkby.lwx.common.XmppTool;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
/**
 * register
 * @author liaowuxing
 *
 */
public class RegistTask {

	private Context context;
	public RegistTask(Context context) {
		this.context = context;
	}
	
	public String getExecute(String username,String password){
		AsyncTask<String, Void, String> result = new ExecuteAsync().execute(username,password);
	    try {
			String state = result.get();
			if (state == Constant.regist_fail) {
				return Constant.regist_fail;
			} else if (state == Constant.regist_success) {
				return Constant.regist_success;
			} else if (state == Constant.ServiceNoResponse) {
				return Constant.ServiceNoResponse;
			} else if (state == Constant.usernameIsExist) {
				return Constant.usernameIsExist;
			} else {
				return Constant.regist_fail;
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			return null;
		} catch (ExecutionException e) {
			e.printStackTrace();
			return null;
		}
	    
	}
	public class ExecuteAsync extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			String state = XmppTool.regist(params[0], params[1]);
			return state;
		}
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null) {
				if (result == Constant.regist_fail) {
					Toast.makeText(context, R.string.regist_fail, Toast.LENGTH_SHORT).show();
				} else if (result == Constant.regist_success) {
					Toast.makeText(context, R.string.regist_success, Toast.LENGTH_SHORT).show();
				} else if (result == Constant.ServiceNoResponse) {
					Toast.makeText(context, R.string.ServiceNoResponse, Toast.LENGTH_SHORT).show();
				} else if (result == Constant.usernameIsExist) {
					Toast.makeText(context, R.string.usernameIsExist, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(context, R.string.regist_fail, Toast.LENGTH_SHORT).show();
				}
			}else {
				Toast.makeText(context, R.string.regist_fail, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
5、最关键的代码在下面,我定义了几个常量,0代表注册失败,1代表注册成功,2代表服务器没有响应,3代表注册的用户已存在。

 
<pre name="code" class="java">/**
	 * regist
	 * 
	 * @param username
	 * @param password
	 * @return 
	 * 
	 */
	public static String regist(String username, String password) {
		if (getConnection() == null) {
			return Constant.ServiceNoResponse;
		}
		Registration reg = new Registration();
		reg.setType(IQ.Type.SET);
		reg.setTo(getConnection().getServiceName());
		//Enter the username,Not a JID
		reg.setUsername(username);
		//Enter the password
		reg.setPassword(password);
		//addAttribut Can't be empty
		reg.addAttribute("android", "geolo_createUser_android");
		PacketFilter filter = new AndFilter(new PacketIDFilter(
				reg.getPacketID()), new PacketTypeFilter(IQ.class));
		PacketCollector collector = getConnection().createPacketCollector(
				filter);
		getConnection().sendPacket(reg);
		IQ result = (IQ) collector.nextResult(SmackConfiguration
				.getPacketReplyTimeout());
		// Stop request results
		collector.cancel();
		if (result == null) {
			return Constant.ServiceNoResponse;
		} else if (result.getType() == IQ.Type.RESULT) {
			return Constant.regist_success;
		} else {
			if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
				return Constant.usernameIsExist;
			} else {
				return Constant.regist_fail;
			}
		}
	}
6、常量当然放在Constant.java里了。
 
<pre name="code" class="java">public static final String regist_fail = "0";//regist fail,
	public static final String regist_success = "1";//regist success,
	public static final String ServiceNoResponse = "2";//service no response
	public static final String usernameIsExist = "3";// username is exist

7、Android工程的结构如下:


8、ok,你可以随便注册一个用户see一下了。see you. 再见

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值