I'm working on an app and I am having some trouble getting the login and registration to work. Here is the code I have for my registration.
public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
//JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//String name = inputFullName.getText().toString();
//String email = inputEmail.getText().toString();
//String password = inputPassword.getText().toString();
new RegisterTask().execute();
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
// Close Registration View
finish();
}
});
}
class RegisterTask extends AsyncTask<String, String, String> {
int success = 0;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String name = inputFullName.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
Log.d("Create Response", json.toString());
try {
success = json.getInt(KEY_SUCCESS);
if (success == 1) {
// DATABASE HANDLER
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(
getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME),
json_user.getString(KEY_EMAIL),
json_user.getString(KEY_UID),
json_user.getString(KEY_CREATED_AT));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (success == 1) {
Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
finish();
}else{
//tvLError.setText("Incorrect username/password");
}
}
}
}
And I am having trouble with my php script as well, I'm not sure what is wrong with that.
<?php
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = mysql_query("SELECT email from users WHERE email = '$email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
And here is the error log I am getting.
12-02 20:45:23.135: E/JSON(28208): <br />n<b>Warning</b>: mysql_num_rows() expects parameter 1 to be resource, boolean given in <b>/home/content/09/9443509/html/gotoclass/DB_Functions.php</b> on line <b>71</b><br />n{"tag":"register","success":0,"error":1,"error_msg":"Error occured in Registartion"}n
12-02 20:45:23.140: E/JSON Parser(28208): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
12-02 20:45:23.140: W/dalvikvm(28208): threadid=13: thread exiting with uncaught exception (group=0x415e62a0)
12-02 20:45:23.140: E/AndroidRuntime(28208): FATAL EXCEPTION: AsyncTask #1
12-02 20:45:23.140: E/AndroidRuntime(28208): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.lang.Thread.run(Thread.java:856)
12-02 20:45:23.140: E/AndroidRuntime(28208): Caused by: java.lang.NullPointerException
12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:89)
12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:1)
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 20:45:23.140: E/AndroidRuntime(28208): ... 5 more
Thank you for any help you guys can offer!!!