Advertisement
markella92

Untitled

Dec 14th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. public class RegisterActivity extends Activity {
  2. private static final String TAG = RegisterActivity.class.getSimpleName();
  3. private Button btnRegister;
  4. private Button btnLinkToLogin;
  5. private EditText inputFullName;
  6. private EditText inputEmail;
  7. private EditText inputPassword;
  8. private ProgressDialog pDialog;
  9. private SessionManager session;
  10. private SQLiteHandler db;
  11.  
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_register);
  16.  
  17. inputFullName = (EditText) findViewById(R.id.name);
  18. inputEmail = (EditText) findViewById(R.id.email);
  19. inputPassword = (EditText) findViewById(R.id.password);
  20. btnRegister = (Button) findViewById(R.id.btnRegister);
  21. btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
  22.  
  23. // Progress dialog
  24. pDialog = new ProgressDialog(this);
  25. pDialog.setCancelable(false);
  26.  
  27. // Session manager
  28. session = new SessionManager(getApplicationContext());
  29.  
  30. // SQLite database handler
  31. db = new SQLiteHandler(getApplicationContext());
  32.  
  33. // Check if user is already logged in or not
  34. if (session.isLoggedIn()) {
  35. // User is already logged in. Take him to main activity
  36. Intent intent = new Intent(RegisterActivity.this,
  37. MainActivity.class);
  38. startActivity(intent);
  39. finish();
  40. }
  41.  
  42. // Register Button Click event
  43. btnRegister.setOnClickListener(new View.OnClickListener() {
  44. public void onClick(View view) {
  45. String name = inputFullName.getText().toString().trim();
  46. String email = inputEmail.getText().toString().trim();
  47. String password = inputPassword.getText().toString().trim();
  48.  
  49. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  50. registerUser(name, email, password);
  51. } else {
  52. Toast.makeText(getApplicationContext(),
  53. "Please enter your details!", Toast.LENGTH_LONG)
  54. .show();
  55. }
  56. }
  57. });
  58.  
  59. // Link to Login Screen
  60. btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
  61.  
  62. public void onClick(View view) {
  63. Intent i = new Intent(getApplicationContext(),
  64. LoginActivity.class);
  65. startActivity(i);
  66. finish();
  67. }
  68. });
  69.  
  70. }
  71.  
  72. /**
  73. * Function to store user in MySQL database will post params(tag, name,
  74. * email, password) to register url
  75. * */
  76. private void registerUser(final String name, final String email,
  77. final String password) {
  78. // Tag used to cancel the request
  79. String tag_string_req = "req_register";
  80.  
  81. pDialog.setMessage("Registering ...");
  82. showDialog();
  83.  
  84. StringRequest strReq = new StringRequest(Method.POST,
  85. AppConfig.URL_REGISTER, new Response.Listener<String>() {
  86.  
  87. @Override
  88. public void onResponse(String response) {
  89. Log.d(TAG, "Register Response: " + response.toString());
  90. hideDialog();
  91.  
  92. try {
  93. JSONObject jObj = new JSONObject(response);
  94. boolean error = jObj.getBoolean("error");
  95. if (!error) {
  96. // User successfully stored in MySQL
  97. // Now store the user in sqlite
  98. String uid = jObj.getString("uid");
  99.  
  100. JSONObject user = jObj.getJSONObject("user");
  101. String name = user.getString("name");
  102. String email = user.getString("email");
  103. String created_at = user
  104. .getString("created_at");
  105.  
  106. // Inserting row in users table
  107. db.addUser(name, email, uid, created_at);
  108.  
  109. Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
  110.  
  111. // Launch login activity
  112. Intent intent = new Intent(
  113. RegisterActivity.this,
  114. LoginActivity.class);
  115. startActivity(intent);
  116. finish();
  117. } else {
  118.  
  119. // Error occurred in registration. Get the error
  120. // message
  121. String errorMsg = jObj.getString("error_msg");
  122. Toast.makeText(getApplicationContext(),
  123. errorMsg, Toast.LENGTH_LONG).show();
  124. }
  125. } catch (JSONException e) {
  126. e.printStackTrace();
  127. }
  128.  
  129. }
  130. }, new Response.ErrorListener() {
  131.  
  132. @Override
  133. public void onErrorResponse(VolleyError error) {
  134. Log.e(TAG, "Registration Error: " + error.getMessage());
  135. Toast.makeText(getApplicationContext(),
  136. error.getMessage(), Toast.LENGTH_LONG).show();
  137. hideDialog();
  138. }
  139. }) {
  140.  
  141. @Override
  142. protected Map<String, String> getParams() {
  143. // Posting params to register url
  144. Map<String, String> params = new HashMap<String, String>();
  145. params.put("name", name);
  146. params.put("email", email);
  147. params.put("password", password);
  148.  
  149. return params;
  150. }
  151.  
  152. };
  153.  
  154. // Adding request to request queue
  155. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  156. }
  157.  
  158. private void showDialog() {
  159. if (!pDialog.isShowing())
  160. pDialog.show();
  161. }
  162.  
  163. private void hideDialog() {
  164. if (pDialog.isShowing())
  165. pDialog.dismiss();
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement