Taimor Taimor - 3 months ago 34
PHP Question

response listener not working correctly

i have a register activity and a login activity i want to open login activity when user successfully register.but in my case i am not getting login activity after registration. user register successfully in data base table.
here is the code of response listener.

private void registerUser(final String name, final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";

pDialog.setMessage("Registering ...");
showDialog();

StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);
hideDialog();

try {
JSONObject jObj = new JSONObject(response);

boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite


JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");

// Inserting row in users table
db.addUser(name, email, created_at);

Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {

// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);

return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}



  • 2 php code

    <?php
    require_once 'include/DB_Functions.php';

    $db = new DB_Functions();

    $response = array("error" => FALSE);


    if (isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) {


    // receiving the REQUEST params
    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];

    $password = $_REQUEST['password'];

    if ($db->isUserExisted($email)) {

    $response["error"] = TRUE;

    $response["error_msg"] = "User already existed with " . $email;

    echo json_encode($response);

    } else {

    $user = $db->storeUser($name, $email, $password);

    if ($user) {

    $response["error"] = FALSE;


    $response["user"]["name"] = $user["name"];

    $response["user"]["email"] = $user["email"];

    $response["user"]["created_at"] = $user["created_at"];

    $response["user"]["updated_at"] = $user["updated_at"];

    echo json_encode($response);

    } else {

    $response["error"] = TRUE;

    $response["error_msg"] = "Unknown error occurred in registration!";

    echo json_encode($response);

    }
    }
    } else {

    $response["error"] = TRUE;

    $response["error_msg"] = "Required parameters (name, email or password) is missing!";

    echo json_encode($response);
    }
    ?>


Answer
## Write the Intent this way ##
Intent intent = new Intent(
                        RegisterActivity.this,
                        LoginActivity.class);
              RegisterActivity.this.startActivity(intent);