Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bummybomo.filmrentalapp.Presentation;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.android.volley.DefaultRetryPolicy;
- import com.android.volley.NetworkResponse;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.example.bummybomo.filmrentalapp.R;
- import com.example.bummybomo.filmrentalapp.Service.Config;
- import com.example.bummybomo.filmrentalapp.Service.VolleyRequestQueue;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by bummybomo on 18-6-2017.
- */
- public class LoginActivity extends AppCompatActivity {
- private EditText editTextEmail;
- private EditText editTextPassword;
- private TextView txtLoginErrorMsg;
- private Button login_btn;
- private String mUsername;
- private String mPassword;
- public final String TAG = this.getClass().getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- editTextEmail = (EditText) findViewById(R.id.editTextEmail);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- txtLoginErrorMsg = (TextView) findViewById(R.id.txtLoginErrorMessage);
- login_btn = (Button) findViewById(R.id.login_btn);
- login_btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mUsername = editTextEmail.getText().toString();
- mPassword = editTextPassword.getText().toString();
- txtLoginErrorMsg.setText("");
- // TODO Checken of username en password niet leeg zijn
- handleLogin(mUsername, mPassword);
- }
- });
- }
- private void handleLogin(String email, String password) {
- //
- // Maak een JSON object met username en password. Dit object sturen we mee
- // als request body (zoals je ook met Postman hebt gedaan)
- //
- try {
- JSONObject jsonBody = new JSONObject();
- jsonBody.put("email", editTextEmail.getText().toString());
- jsonBody.put("password", editTextPassword.getText().toString());
- JsonObjectRequest jsObjRequest = new JsonObjectRequest
- (Request.Method.POST, Config.URL_LOGIN, jsonBody, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- // Succesvol response - dat betekent dat we een geldig token hebben.
- // txtLoginErrorMsg.setText("Response: " + response.toString());
- displayMessage("Succesvol ingelogd!");
- // We hebben nu het token. We kiezen er hier voor om
- // het token in SharedPreferences op te slaan. Op die manier
- // is het token tussen app-stop en -herstart beschikbaar -
- // totdat het token expired.
- try {
- String token = response.getString("token");
- Context context = getApplicationContext();
- SharedPreferences sharedPref = context.getSharedPreferences(
- getString(R.string.preference_file_key), Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.putString(getString(R.string.saved_token), token);
- editor.commit();
- // Start the main activity, and close the login activity
- Intent main = new Intent(getApplicationContext(), MainActivity.class);
- startActivity(main);
- // Close the current activity
- finish();
- } catch (JSONException e) {
- // e.printStackTrace();
- Log.e(TAG, e.getMessage());
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- handleErrorResponse(error);
- }
- });
- jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
- 1500, // SOCKET_TIMEOUT_MS,
- 2, // DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- // Access the RequestQueue through your singleton class.
- VolleyRequestQueue.getInstance(this).addToRequestQueue(jsObjRequest);
- } catch (JSONException e) {
- txtLoginErrorMsg.setText(e.getMessage());
- // e.printStackTrace();
- }
- return;
- }
- public void handleErrorResponse(VolleyError error) {
- Log.e(TAG, "handleErrorResponse");
- if(error instanceof com.android.volley.AuthFailureError) {
- String json = null;
- NetworkResponse response = error.networkResponse;
- if (response != null && response.data != null) {
- json = new String(response.data);
- json = trimMessage(json, "error");
- if (json != null) {
- json = "Error " + response.statusCode + ": " + json;
- displayMessage(json);
- }
- } else {
- Log.e(TAG, "handleErrorResponse: kon geen networkResponse vinden.");
- }
- } else if(error instanceof com.android.volley.NoConnectionError) {
- Log.e(TAG, "handleErrorResponse: server was niet bereikbaar");
- txtLoginErrorMsg.setText(getString(R.string.error_server_offline));
- } else {
- Log.e(TAG, "handleErrorResponse: error = " + error);
- }
- }
- public String trimMessage(String json, String key){
- Log.i(TAG, "trimMessage: json = " + json);
- String trimmedString = null;
- try{
- JSONObject obj = new JSONObject(json);
- trimmedString = obj.getString(key);
- } catch(JSONException e){
- e.printStackTrace();
- return null;
- }
- return trimmedString;
- }
- public void displayMessage(String toastString){
- Toast.makeText(getApplicationContext(), toastString, Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement