Advertisement
Guest User

AndroidHive Login/Registration tutorial modification

a guest
Oct 21st, 2013
1,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. 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.
  2.  
  3. In LoginActivity.java, add this class:
  4.  
  5. private class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
  6.  
  7. protected JSONObject doInBackground(String... params) {
  8. UserFunctions userFunction = new UserFunctions();
  9. if (params.length != 2)
  10. return null;
  11. JSONObject json = userFunction.loginUser(params[0], params[1]);
  12. return json;
  13. }
  14.  
  15. protected void onPostExecute(JSONObject json) {
  16. try {
  17. if (json != null && json.getString(KEY_SUCCESS) != null) {
  18. loginErrorMsg.setText("");
  19. String res = json.getString(KEY_SUCCESS);
  20. if(Integer.parseInt(res) == 1){
  21. // user successfully logged in
  22. // Store user details in SQLite Database
  23. DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  24. JSONObject json_user = json.getJSONObject("user");
  25.  
  26. // Clear all previous data in database
  27. UserFunctions userFunction = new UserFunctions();
  28. userFunction.logoutUser(getApplicationContext());
  29. db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
  30.  
  31. // Launch Dashboard Screen
  32. Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
  33.  
  34. // Close all views before launching Dashboard
  35. dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  36. startActivity(dashboard);
  37.  
  38. // Close Login Screen
  39. finish();
  40. }else{
  41. // Error in login
  42. loginErrorMsg.setText("Incorrect username/password");
  43. }
  44. }
  45. } catch (JSONException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. Then, inside the onCreate method, change the login button's OnClickListener so its onClick method looks like this:
  52.  
  53. public void onClick(View view) {
  54. String email = inputEmail.getText().toString();
  55. String password = inputPassword.getText().toString();
  56. new MyAsyncTask().execute(email, password);
  57. }
  58.  
  59. Do the same sort of thing in RegisterActivity.java. Add this class:
  60.  
  61. private class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
  62.  
  63. protected JSONObject doInBackground(String... params) {
  64. UserFunctions userFunction = new UserFunctions();
  65. if (params.length != 3)
  66. return null;
  67. JSONObject json = userFunction.registerUser(params[0], params[1], params[2]);
  68. return json;
  69. }
  70.  
  71. protected void onPostExecute(JSONObject json) {
  72. // check for login response
  73. try {
  74. if (json != null && json.getString(KEY_SUCCESS) != null) {
  75. registerErrorMsg.setText("");
  76. String res = json.getString(KEY_SUCCESS);
  77. if(Integer.parseInt(res) == 1){
  78. // user successfully registred
  79. // Store user details in SQLite Database
  80. DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  81. JSONObject json_user = json.getJSONObject("user");
  82.  
  83. // Clear all previous data in database
  84. UserFunctions userFunction = new UserFunctions();
  85. userFunction.logoutUser(getApplicationContext());
  86. db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
  87. // Launch Dashboard Screen
  88. Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
  89. // Close all views before launching Dashboard
  90. dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  91. startActivity(dashboard);
  92. // Close Registration Screen
  93. finish();
  94. }else{
  95. // Error in registration
  96. registerErrorMsg.setText("Error occured in registration");
  97. }
  98. }
  99. } catch (JSONException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104.  
  105. And change the register button listener's onClick method to this:
  106.  
  107. public void onClick(View view) {
  108. String name = inputFullName.getText().toString();
  109. String email = inputEmail.getText().toString();
  110. String password = inputPassword.getText().toString();
  111. new MyAsyncTask().execute(name, email, password);
  112. }
  113.  
  114. Try running the app now, and it should work!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement