Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If you tried implementing the code from AndroidHive's tutorial (found here: http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/), then you might have gotten a android.os.NetworkOnMainThreadException error when attempting to log in or register via the phone app. To fix this, you must use AsyncTasks to run the code that retrieves info from the website.
- In LoginActivity.java, add this class:
- private class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
- protected JSONObject doInBackground(String... params) {
- UserFunctions userFunction = new UserFunctions();
- if (params.length != 2)
- return null;
- JSONObject json = userFunction.loginUser(params[0], params[1]);
- return json;
- }
- protected void onPostExecute(JSONObject json) {
- try {
- if (json != null && json.getString(KEY_SUCCESS) != null) {
- loginErrorMsg.setText("");
- String res = json.getString(KEY_SUCCESS);
- if(Integer.parseInt(res) == 1){
- // 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
- UserFunctions userFunction = new UserFunctions();
- userFunction.logoutUser(getApplicationContext());
- db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
- // Launch Dashboard Screen
- Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
- // Close all views before launching Dashboard
- dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(dashboard);
- // Close Login Screen
- finish();
- }else{
- // Error in login
- loginErrorMsg.setText("Incorrect username/password");
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- Then, inside the onCreate method, change the login button's OnClickListener so its onClick method looks like this:
- public void onClick(View view) {
- String email = inputEmail.getText().toString();
- String password = inputPassword.getText().toString();
- new MyAsyncTask().execute(email, password);
- }
- Do the same sort of thing in RegisterActivity.java. Add this class:
- private class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
- protected JSONObject doInBackground(String... params) {
- UserFunctions userFunction = new UserFunctions();
- if (params.length != 3)
- return null;
- JSONObject json = userFunction.registerUser(params[0], params[1], params[2]);
- return json;
- }
- protected void onPostExecute(JSONObject json) {
- // check for login response
- try {
- if (json != null && json.getString(KEY_SUCCESS) != null) {
- registerErrorMsg.setText("");
- String res = json.getString(KEY_SUCCESS);
- if(Integer.parseInt(res) == 1){
- // user successfully registred
- // Store user details in SQLite Database
- DatabaseHandler db = new DatabaseHandler(getApplicationContext());
- JSONObject json_user = json.getJSONObject("user");
- // Clear all previous data in database
- UserFunctions userFunction = new UserFunctions();
- userFunction.logoutUser(getApplicationContext());
- db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
- // Launch Dashboard Screen
- Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
- // Close all views before launching Dashboard
- dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(dashboard);
- // Close Registration Screen
- finish();
- }else{
- // Error in registration
- registerErrorMsg.setText("Error occured in registration");
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- And change the register button listener's onClick method to this:
- public void onClick(View view) {
- String name = inputFullName.getText().toString();
- String email = inputEmail.getText().toString();
- String password = inputPassword.getText().toString();
- new MyAsyncTask().execute(name, email, password);
- }
- Try running the app now, and it should work!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement