当我按下注册而不在字段中键入任何内容时,它不会显示来自try catch块的警告框。此外,当电子邮件已经在数据库中时,它应该显示警告框但它不起作用。
public class activityregister extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText register_age = (EditText) findViewById(R.id.register_age);
final EditText register_email = (EditText) findViewById(R.id.emails_reg);
final EditText register_name = (EditText) findViewById(R.id.register_name);
final EditText register_password = (EditText) findViewById(R.id.register_password);
final Button reg_poga = (Button) findViewById(R.id.registacijas_poga);
reg_poga.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = register_name.getText().toString();
final String email = register_email.getText().toString();
final String age = register_age.getText().toString();
final String password = register_password.getText().toString();
boolean pareizi = false;
final Response.Listener <String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success"
);
if (success) {
Intent intent = new Intent(activityregister.this, LoginRegister.class);
activityregister.this.startActivity(intent);
}
} catch (JSONException e){
AlertDialog.Builder builder = new AlertDialog.Builder(activityregister.this);
builder.setMessage("Register is not completed!")
.setNegativeButton("Retry the registration!",null)
.create()
.show();
e.printStackTrace();
}
}
};
RegisterRequest registerRequestt= new RegisterRequest(name, email, age, password, responseListener);
RequestQueue Que = Volley.newRequestQueue(activityregister.this);
Que.add(registerRequestt);
}
});
}
}
答案 0 :(得分:0)
您需要在catch块中使用 Exception 代替 JSONException 。
如果你在catch块中传递 Exception ,那么它将捕获所有类型的异常,因为Exception是所有异常类型的Parent类
现在您正在使用 JSONException ,因此它只会捕获与JSON相关的错误。
在你的代码中使用这样的
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success"
);
if (success) {
Intent intent = new Intent(activityregister.this, LoginRegister.class);
activityregister.this.startActivity(intent);
}
} catch (Exception e){
AlertDialog.Builder builder = new AlertDialog.Builder(activityregister.this);
builder.setMessage("Register is not completed!")
.setNegativeButton("Retry the registration!",null)
.create()
.show();
e.printStackTrace();
}
根据你的第二个要求,如果电子邮件是空的等等,我在这里给你的完整代码进行更正
public class activityregister extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText register_age = (EditText) findViewById(R.id.register_age);
final EditText register_email = (EditText) findViewById(R.id.emails_reg);
final EditText register_name = (EditText) findViewById(R.id.register_name);
final EditText register_password = (EditText) findViewById(R.id.register_password);
final Button reg_poga = (Button) findViewById(R.id.registacijas_poga);
reg_poga.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = register_name.getText().toString();
final String email = register_email.getText().toString();
final String age = register_age.getText().toString();
final String password = register_password.getText().toString();
boolean pareizi = false;
if(TextUtils.isEmpty(email)){
// Here show alert or show toast , if email is empty
} else {
final Response.Listener <String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success"
);
if (success) {
Intent intent = new Intent(activityregister.this, LoginRegister.class);
activityregister.this.startActivity(intent);
}
} catch (JSONException e){
AlertDialog.Builder builder = new AlertDialog.Builder(activityregister.this);
builder.setMessage("Register is not completed!")
.setNegativeButton("Retry the registration!",null)
.create()
.show();
e.printStackTrace();
}
}
};
RegisterRequest registerRequestt= new RegisterRequest(name, email, age, password, responseListener);
RequestQueue Que = Volley.newRequestQueue(activityregister.this);
Que.add(registerRequestt);
}
}});
}
}