Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_login);
  5.  
  6. email = (EditText) findViewById(R.id.email);
  7. password = (EditText) findViewById(R.id.password);
  8. login = (Button) findViewById(R.id.login);
  9. signup = (TextView) findViewById(R.id.open_signup);
  10. progressDialog = new ProgressDialog(this);
  11. session = new UserSession(this);
  12. userInfo = new UserInfo(this);
  13.  
  14. if (session.isUserLoggedin()) {
  15. startActivity(new Intent(this, MainActivity.class));
  16. finish();
  17.  
  18. }
  19.  
  20. login.setOnClickListener(this);
  21. signup.setOnClickListener(this);
  22.  
  23. }
  24.  
  25. private void login(final String email, final String password) {
  26. //Tag used to cancel the request
  27. String tag_string_req = "req_login";
  28. progressDialog.setMessage("Logging in....");
  29. progressDialog.show();
  30.  
  31. StringRequest strReq = new StringRequest(Request.Method.POST,
  32. Utils.LOGIN_URL, new Response.Listener<String>() {
  33.  
  34. @Override
  35. public void onResponse(String response) {
  36. Log.d(TAG, "Login Response: " + response.toString());
  37.  
  38. try {
  39. JSONObject jObj = new JSONObject(response);
  40. boolean error = jObj.getBoolean("error");
  41.  
  42. //check for error node in json
  43. if (!error) {
  44. //now store the user in SQLite
  45. JSONObject user = jObj.getJSONObject("user");
  46. String uName = user.getString("username");
  47. String email = user.getString("email");
  48.  
  49. //Inserting row in users table
  50. userInfo.setEmail(email);
  51. userInfo.setUsername(uName);
  52. session.setLoggedin(true);
  53.  
  54. startActivity(new Intent(Login.this, MainActivity.class));
  55. finish();
  56. } else {
  57. //error in login, get the error message
  58. String errorMsg = jObj.getString("error_msg");
  59. toast(errorMsg);
  60. }
  61. } catch (JSONException e) {
  62. //json error
  63. e.printStackTrace();
  64. toast("Json error: " + e.getMessage());
  65.  
  66. }
  67. }
  68. }, new Response.ErrorListener() {
  69.  
  70. @Override
  71. public void onErrorResponse(VolleyError error) {
  72. Log.e(TAG, "Login Error: " + error.getMessage());
  73. toast("Unknown Error occured");
  74. progressDialog.hide();
  75. }
  76.  
  77. }) {
  78. @Override
  79. protected Map<String, String> getParams() {
  80. //Posting parameters to login url
  81. Map<String, String> params = new HashMap<>();
  82. params.put("email", email);
  83. params.put("password", password);
  84.  
  85. return params;
  86.  
  87. }
  88.  
  89. ;
  90.  
  91. //Adding request to request queue
  92. AndroidLoginController.getInstance();
  93.  
  94. addToRequestQueue(strReq, tag_string_req);
  95. }
  96.  
  97. private void toast(String x) {
  98. Toast.makeText(this, x, Toast.LENGTH_SHORT).show();
  99. }
  100.  
  101. @Override
  102. public void onClick(View v) {
  103. switch (v.getId()) {
  104. case R.id.login:
  105. String uName = email.getText().toString().trim();
  106. String pass = password.getText().toString().trim();
  107.  
  108. login(uName, pass);
  109. break;
  110. case R.id.open_signup:
  111. startActivity(new Intent(this, SignUp.class));
  112. break;
  113.  
  114. }
  115. }
  116.  
  117. @Override
  118. protected void onCreate(Bundle savedInstanceState) {
  119. super.onCreate(savedInstanceState);
  120.  
  121. // ATTENTION: This was auto-generated to implement the App Indexing API.
  122. // See https://g.co/AppIndexing/AndroidStudio for more information.
  123. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  124. }
  125.  
  126. /**
  127. * ATTENTION: This was auto-generated to implement the App Indexing API.
  128. * See https://g.co/AppIndexing/AndroidStudio for more information.
  129. */
  130. public Action getIndexApiAction() {
  131. Thing object = new Thing.Builder()
  132. .setName("Login Page") // TODO: Define a title for the content shown.
  133. // TODO: Make sure this auto-generated URL is correct.
  134. .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
  135. .build();
  136. return new Action.Builder(Action.TYPE_VIEW)
  137. .setObject(object)
  138. .setActionStatus(Action.STATUS_TYPE_COMPLETED)
  139. .build();
  140. }
  141.  
  142. @Override
  143. public void onStart() {
  144. super.onStart();
  145.  
  146. // ATTENTION: This was auto-generated to implement the App Indexing API.
  147. // See https://g.co/AppIndexing/AndroidStudio for more information.
  148. client.connect();
  149. AppIndex.AppIndexApi.start(client, getIndexApiAction());
  150. }
  151.  
  152. @Override
  153. public void onStop() {
  154. super.onStop();
  155.  
  156. // ATTENTION: This was auto-generated to implement the App Indexing API.
  157. // See https://g.co/AppIndexing/AndroidStudio for more information.
  158. AppIndex.AppIndexApi.end(client, getIndexApiAction());
  159. client.disconnect();
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement