Advertisement
Guest User

login

a guest
Jun 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. package com.example.bummybomo.filmrentalapp.Presentation;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import com.android.volley.DefaultRetryPolicy;
  16. import com.android.volley.NetworkResponse;
  17. import com.android.volley.Request;
  18. import com.android.volley.Response;
  19. import com.android.volley.VolleyError;
  20. import com.android.volley.toolbox.JsonObjectRequest;
  21. import com.example.bummybomo.filmrentalapp.R;
  22. import com.example.bummybomo.filmrentalapp.Service.Config;
  23. import com.example.bummybomo.filmrentalapp.Service.VolleyRequestQueue;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27. /**
  28. * Created by bummybomo on 18-6-2017.
  29. */
  30.  
  31. public class LoginActivity extends AppCompatActivity {
  32.  
  33. private EditText editTextEmail;
  34. private EditText editTextPassword;
  35. private TextView txtLoginErrorMsg;
  36. private Button login_btn;
  37.  
  38. private String mUsername;
  39. private String mPassword;
  40.  
  41. public final String TAG = this.getClass().getSimpleName();
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_login);
  47.  
  48. editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  49. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  50. txtLoginErrorMsg = (TextView) findViewById(R.id.txtLoginErrorMessage);
  51. login_btn = (Button) findViewById(R.id.login_btn);
  52. login_btn.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. mUsername = editTextEmail.getText().toString();
  56. mPassword = editTextPassword.getText().toString();
  57. txtLoginErrorMsg.setText("");
  58.  
  59. // TODO Checken of username en password niet leeg zijn
  60.  
  61. handleLogin(mUsername, mPassword);
  62. }
  63. });
  64. }
  65.  
  66. private void handleLogin(String email, String password) {
  67. //
  68. // Maak een JSON object met username en password. Dit object sturen we mee
  69. // als request body (zoals je ook met Postman hebt gedaan)
  70. //
  71.  
  72. try {
  73. JSONObject jsonBody = new JSONObject();
  74. jsonBody.put("email", editTextEmail.getText().toString());
  75. jsonBody.put("password", editTextPassword.getText().toString());
  76. JsonObjectRequest jsObjRequest = new JsonObjectRequest
  77. (Request.Method.POST, Config.URL_LOGIN, jsonBody, new Response.Listener<JSONObject>() {
  78.  
  79. @Override
  80. public void onResponse(JSONObject response) {
  81. // Succesvol response - dat betekent dat we een geldig token hebben.
  82. // txtLoginErrorMsg.setText("Response: " + response.toString());
  83. displayMessage("Succesvol ingelogd!");
  84.  
  85. // We hebben nu het token. We kiezen er hier voor om
  86. // het token in SharedPreferences op te slaan. Op die manier
  87. // is het token tussen app-stop en -herstart beschikbaar -
  88. // totdat het token expired.
  89. try {
  90. String token = response.getString("token");
  91.  
  92. Context context = getApplicationContext();
  93. SharedPreferences sharedPref = context.getSharedPreferences(
  94. getString(R.string.preference_file_key), Context.MODE_PRIVATE);
  95. SharedPreferences.Editor editor = sharedPref.edit();
  96. editor.putString(getString(R.string.saved_token), token);
  97. editor.commit();
  98.  
  99. // Start the main activity, and close the login activity
  100. Intent main = new Intent(getApplicationContext(), MainActivity.class);
  101. startActivity(main);
  102. // Close the current activity
  103. finish();
  104.  
  105. } catch (JSONException e) {
  106. // e.printStackTrace();
  107. Log.e(TAG, e.getMessage());
  108. }
  109. }
  110. }, new Response.ErrorListener() {
  111.  
  112. @Override
  113. public void onErrorResponse(VolleyError error) {
  114. handleErrorResponse(error);
  115. }
  116. });
  117.  
  118. jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
  119. 1500, // SOCKET_TIMEOUT_MS,
  120. 2, // DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  121. DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  122.  
  123. // Access the RequestQueue through your singleton class.
  124. VolleyRequestQueue.getInstance(this).addToRequestQueue(jsObjRequest);
  125. } catch (JSONException e) {
  126. txtLoginErrorMsg.setText(e.getMessage());
  127. // e.printStackTrace();
  128. }
  129. return;
  130. }
  131.  
  132. public void handleErrorResponse(VolleyError error) {
  133. Log.e(TAG, "handleErrorResponse");
  134.  
  135. if(error instanceof com.android.volley.AuthFailureError) {
  136.  
  137. String json = null;
  138. NetworkResponse response = error.networkResponse;
  139. if (response != null && response.data != null) {
  140. json = new String(response.data);
  141. json = trimMessage(json, "error");
  142. if (json != null) {
  143. json = "Error " + response.statusCode + ": " + json;
  144. displayMessage(json);
  145. }
  146. } else {
  147. Log.e(TAG, "handleErrorResponse: kon geen networkResponse vinden.");
  148. }
  149. } else if(error instanceof com.android.volley.NoConnectionError) {
  150. Log.e(TAG, "handleErrorResponse: server was niet bereikbaar");
  151. txtLoginErrorMsg.setText(getString(R.string.error_server_offline));
  152. } else {
  153. Log.e(TAG, "handleErrorResponse: error = " + error);
  154. }
  155. }
  156.  
  157. public String trimMessage(String json, String key){
  158. Log.i(TAG, "trimMessage: json = " + json);
  159. String trimmedString = null;
  160.  
  161. try{
  162. JSONObject obj = new JSONObject(json);
  163. trimmedString = obj.getString(key);
  164. } catch(JSONException e){
  165. e.printStackTrace();
  166. return null;
  167. }
  168. return trimmedString;
  169. }
  170.  
  171. public void displayMessage(String toastString){
  172. Toast.makeText(getApplicationContext(), toastString, Toast.LENGTH_LONG).show();
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement