我正在尝试为Android系统创建一个注册页面。我需要检查它的验证,就像email_id已经存在,密码应该包含超过8个字符,手机号码字段应该包含最多10位数。我怎么能这样做?这是我的代码。我使用java rest webservices和Mysql数据库
注册.java
package com.example.locationapp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@SuppressWarnings("deprecation")
public class SignUp extends Activity {
EditText et_name, et_email, et_password, et_conf_password, et_number;
Button signup;
private Context context;
private ProgressDialog pDialog;
private CheckInternet checkNet;
private Pattern pattern;
private Matcher matcher;
private String EMAIL_PATTERN, name, email, password, conf_password, number,
toastMessage;
private boolean emailValidity, internet_status;
private final String TAG_RESPONSE_CODE = "resp_code";
ServiceHandle jsonParser = new ServiceHandle();
private String url = "http://172.30.54.89:8080/LocationBasedFramework/SignUp/RegisterUser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
context = SignUp.this;
et_name = (EditText) findViewById(R.id.nameEditText);
et_email = (EditText) findViewById(R.id.emailEditText);
et_password = (EditText) findViewById(R.id.passwordEditText);
et_conf_password = (EditText) findViewById(R.id.conf_passwordEditText);
et_number = (EditText) findViewById(R.id.numberEditText);
signup = (Button) findViewById(R.id.signUpbutton);
checkNet = new CheckInternet();
EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
pDialog = new ProgressDialog(SignUp.this);
pDialog.setMessage("New User...");
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// CheckInternet
name = et_name.getText().toString();
email = et_email.getText().toString();
password = et_password.getText().toString();
conf_password = et_conf_password.getText().toString();
number = et_number.getText().toString();
if (email.equals("")) {
toastMessage = "Email field cannot be empty";
et_email.requestFocus(1);
} else {
emailValidity = validate(email);
if (!emailValidity) {
toastMessage = "Invalid E_mail id";
et_email.setText("");
checkNet.ToastMessage(toastMessage,
getApplicationContext());
System.out.println("onclick 3.5");
et_email.requestFocus(1);
} else {
System.out.println("-------onClick4-------");
if (password.equals("")) {
toastMessage = "Password field cannot be empty";
checkNet.ToastMessage(toastMessage, context);
et_conf_password.setText("");
et_password.requestFocus(1);
} else {
if (conf_password.equals("")) {
toastMessage = "Please confirm your password";
checkNet.ToastMessage(toastMessage, context);
et_conf_password.requestFocus(1);
} else {
if (name.equals("")) {
toastMessage = "Plaese enter your name";
checkNet.ToastMessage(toastMessage, context);
et_name.requestFocus(1);
} else {
if (number.equals("")) {
toastMessage = "Mobile No field cannot be empty";
checkNet.ToastMessage(toastMessage,
context);
et_number.requestFocus(1);
} else {
System.out
.println("-------onClick8-------");
internet_status = checkNet
.isInternetOn(context);
if (internet_status == true) {
System.out
.println("-------onClick9-------");
new CreateUser().execute();
} else {
System.out
.println("-------onClick10-------");
checkNet.ifNoInternet(context);
}
}
}
}
}
}
}
}
});
}
public boolean validate(String email) {
CharSequence hex = null;
matcher = pattern.matcher(email);
return matcher.matches();
}
class CreateUser extends AsyncTask<String, String, String> {
StringEntity se = null;
JSONObject json;
String jsonStr = "";
Boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
protected String doInBackground(String... params) {
try {
json = new JSONObject();
json.put("name", name);
json.put("email_id", email);
json.put("password", password);
json.put("mob_num", number);
se = new StringEntity(json.toString());
System.out.println("--after se--");
String method = "post";
// int response_flag = 1;
ServiceHandle sh = new ServiceHandle();
internet_status = checkNet
.isInternetOn(getApplicationContext());
System.out.println("--internet--" + internet_status);
if (internet_status == true) {
jsonStr = sh.makeServiceCall(url, method, se);
} else {
checkNet.ifNoInternet(context);
}
if (jsonStr != null) {
json = new JSONObject(jsonStr);
int resp_code = json.getInt(TAG_RESPONSE_CODE);
if (resp_code == 0) {
name = json.getString("TAG_Name");
email = json.getString("TAG_Email");
password = json.getString("TAG_Password");
conf_password = json.getString("TAG_confPassword");
number = json.getString("TAG_Number");
}
} else {
;
}
}
catch (Exception e) {
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
pDialog.cancel();
int response_flag = 0;
if (response_flag == 0) {
int resp_code = 0;
if (resp_code == 0) {
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("userData", MODE_PRIVATE);
Editor editor = pref.edit();
editor.clear();
editor.commit();
editor.putString("name", name);
editor.putString("e_mail", email);
editor.putString("password", password);
editor.putString("conf_password", conf_password);
editor.putString("number", number);
editor.commit();
Intent intent = new Intent(SignUp.this, ViewMap.class);
startActivity(intent);
finish();
}
}
} catch (Exception e) {
}
}
}
}
这是SignUp.java的xml。
activity_sign_up.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.locationapp.SignUp" >
<TextView
android:id="@+id/NametextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/emailtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="Email-id"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/passwordtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:gravity="center"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/conf_passwordtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:gravity="center"
android:text="Confirm Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/numbertextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:gravity="center"
android:text="Mobile"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/NametextView"
android:layout_alignBottom="@+id/NametextView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/passwordtextView"
android:background="#FFFFFF"
android:ems="10" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/emailtextView"
android:layout_alignBottom="@+id/emailtextView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/passwordtextView"
android:background="#FFFFFF"
android:ems="10" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/passwordtextView"
android:layout_alignBottom="@+id/passwordtextView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/passwordtextView"
android:background="#FFFFFF"
android:ems="10"
android:inputType="textPassword" />
<EditText
android:id="@+id/conf_passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/conf_passwordtextView"
android:layout_alignBottom="@+id/passwordtextView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/conf_passwordtextView"
android:background="#FFFFFF"
android:ems="10"
android:inputType="textPassword"/>
<EditText
android:id="@+id/numberEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/numbertextView"
android:layout_alignBottom="@+id/numbertextView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/passwordtextView"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/signUpbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/numbertextView"
android:layout_marginTop="34dp"
android:layout_toRightOf="@+id/emailtextView"
android:gravity="center"
android:text="SIGNUP" />
</RelativeLayout>
答案 0 :(得分:1)
检查电子邮件的验证,例如email_id已存在 ...从表单服务器管理
密码应包含超过8个字符,手机号码字段应包含最多10位
只需将密码传递给以下给定的mathod
即可private boolean isValidEmail(String email){
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}