I recently lost my work due to hardware failure. I have cloned the Android project from GitHub and rewrote the PHP scripts and rewrote the database but the registration function I have written is no longer working, despite working before the failure.
I have located the error in the logcat but cannot work out what is happening. It is in this area of the code:
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Register Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
and the logcat reads:
04-21 12:57:42.476 26530-26530/com.example.xxx.xxxxxE/RegisterActivity: Login Error: null
When I follow the url in the logcat it is the response that required parameters are missing from the PHP script.
{"error":true,"error_msg":"Required parameters (first name, last name, handicap, email or password) is missing!"}
so it is reaching the PHP.
The rest of the code is here:
Android Register Activity:
public class RegisterActivity extends Activity {
//Variable declarations
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnReg;
private Button btnLinkToLogin;
private EditText inputFirstName;
private EditText inputLastName;
private EditText inputEmail;
private EditText inputPassword;
private EditText inputConfirmPassword;
private EditText inputHandicap;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputFirstName = (EditText) findViewById(R.id.firstName);
inputLastName = (EditText) findViewById(R.id.lastName);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
inputConfirmPassword = (EditText) findViewById(R.id.confirmPassword);
inputHandicap = (EditText) findViewById(R.id.handicap);
btnReg = (Button) findViewById(R.id.btnReg);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLogin);
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
session= new SessionManager(getApplicationContext());
db = new SQLiteHandler(getApplicationContext());
if(session.isLoggedIn()){
Intent intent = new Intent(RegisterActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
btnReg.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String firstName = inputFirstName.getText().toString().trim();
String lastName = inputLastName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
String handicap = inputHandicap.getText().toString().trim();
if(inputPassword.getText().toString().equals(inputConfirmPassword.getText().toString())){
if(!firstName.isEmpty() && !lastName.isEmpty() && !email.isEmpty() &&
!password.isEmpty() && !handicap.isEmpty()){
registerUser(firstName, lastName, email, password, handicap);
}else{
Toast.makeText(getApplicationContext(),
"Please check you have entered the correct details", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_LONG).show();
}
}
});
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
finish();
}
});
}
private void registerUser(final String firstName, final String lastName, final String email, final String password, final String handicap) {
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.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String firstName = user.getString("firstName");
String lastName = user.getString("lastName");
String email = user.getString("email");
int handicap = user.getInt("handicap");
db.addUser(firstName, lastName, uid, email, handicap);
Toast.makeText(getApplicationContext(), "Account successfully created. You may now log in", Toast.LENGTH_LONG).show();
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
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, "Register Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}){
@Override
protected Map<String, String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("firstName", firstName);
params.put("lastName", lastName);
params.put("email", email);
params.put("password", password);
params.put("handicap", handicap);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
finish();
}
private void showDialog() {
if(!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if(pDialog.isShowing())
pDialog.dismiss();
}
}
PHP Script
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("error" => FALSE);
if(isset($_POST['firstName']) && isset($_POST['lastName']) &&
isset($_POST['email']) && isset($_POST['handicap']) &&
isset($_POST['password'])){
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$handicap = $_POST['handicap'];
$password = $_POST['password'];
if($db->userExists($email)){
$response["error"] = TRUE;
$response["error_msg"] = "Email taken";
echo json_encode($response);
}else{
$user = $db->storeUser($firstName, $lastName, $email, $handicap, $password);
if($user){
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["firstName"] = $user["first_name"];
$response["user"]["lastName"] = $user["last_name"];
$response["user"]["email"] = $user["email"];
$response["user"]["handicap"] = $user["handicap"];
echo json_encode($response);
}else{
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error";
echo json_encode($response);
}
}
}else{
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (first name, last name, handicap, email or password) is missing!";
echo json_encode($response);
}
?>