首页 > 解决方案 > 成功注册到应用程序后,Android Activity 没有变化

问题描述

我创建了一个具有注册活动的应用程序。当我点击应用程序上的注册按钮时,用户的详细信息被插入到数据库中,但活动不会改变。对于应用程序上的登录,活动按预期从登录活动更改为主页活动,但对于注册活动则没有。

这是代码:

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
                @Override
                public void onResponse(String response) {
                    Toast.makeText(register.this, response, Toast.LENGTH_SHORT).show();

                    if (response.equals("success")) {
                        Toast.makeText(register.this, response, Toast.LENGTH_LONG).show();
                        btnRegister.setClickable(false);

                        Intent intent = new Intent(register.this, MainActivity.class);
                        startActivity(intent);
                        finish();

                    } else if (response.equals("failure")) {
                        tvStatus.setText("Registration unsuccesful!");
                        Toast.makeText(register.this, response, Toast.LENGTH_SHORT).show();

                    }
                }
            }

标签: javaandroidandroid-activityandroid-volley

解决方案


在我看来,您的回答可能不是您所期望的,我没有使用您使用的侦听器,但是通过查看其他一些问题,我发现了这一点:

JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");

所以请检查这是否适合你,而不是

if (response.equals("success")) {

这个:

JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
}else{
}

您可能需要围绕它添加一个 try/catch。


推荐阅读