Dr.*_*.TZ -1 java validation android login android-volley
我在 Android 中开发了一种登录表单。我必须在我的登录类中实现验证部分。这是我的登录类。我正在使用 api 连接数据库。请帮我这个。
公共类 LoginActivity 扩展 AppCompatActivity {
private static final int REQUEST_READ_CONTACTS = 0;
SoapPrimitive resultString;
String Response;
String url = "url";
private EditText usernamee;
private EditText passwordd;
private View mLoginFormView;
String username = "";
String password = "";
String response = "";
private String count = "";
JSONArray LocArray = new JSONArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final Context context = getApplicationContext();
// Set up the login form.
usernamee = (EditText) findViewById(R.id.usernamee);
passwordd = (EditText) findViewById(R.id.passwordd);
Button mEmailSignInButton = (Button) findViewById(R.id.btnLogin);
mEmailSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernamee.getText().toString();
password = passwordd.getText().toString();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
try {
JSONObject obj = new JSONObject(response);
if(obj.get("Role").equals("Admin"))
{
Intent adminIntent = new Intent(LoginActivity.this, Main2Activity.class);
startActivity(adminIntent);
}
else if (obj.get("Role").equals("User"))
{
Intent userIntent = new Intent(LoginActivity.this, Main3Activity.class);
startActivity(userIntent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
return params;
//return null;
}
};
queue.add(postRequest);
}
});
}
Run Code Online (Sandbox Code Playgroud)
// @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
}
如何在我的登录类中实现验证。
尝试这个
private EditText input_email,input_password;
public boolean validate() {
boolean valid = true;
String email = input_email.getText().toString();
String password = input_password.getText().toString();
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
input_email.setError("enter a valid email address");
valid = false;
} else {
input_email.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
input_password.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
input_password.setError(null);
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)