Guest User

Untitled

a guest
Apr 15th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. package login.hfad.com;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. import com.android.volley.RequestQueue;
  13. import com.android.volley.Response;
  14. import com.android.volley.toolbox.Volley;
  15.  
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. /**
  20.  * Created by dipak on 24/03/2017.
  21.  * This class is responsible for handling the login functionality of my application.
  22.  * It contains implementation for the "Login" button's event handler that sends users login details
  23.  * to my web server to be verified. It also contains error handling to ensure that all required fields
  24.  * contain a value before attempting to log in to my system. It makes use of Volley library
  25.  * functions to send and receive data to and from my web server.
  26.  */
  27. public class LoginActivity extends AppCompatActivity {
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_login);
  33.  
  34.          /* Declaration and initialisation of final variables that are
  35.             assigned a View from my activity_login XML layout file. */
  36.         final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  37.         final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  38.         final Button btnSignIn = (Button) findViewById(R.id.bLogin);
  39.         final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
  40.  
  41.         //create a setOnClickListener for the "register" text link
  42.         registerLink.setOnClickListener(new View.OnClickListener(){
  43.  
  44.             //implementation for setOnClickListeners , onClick method
  45.             @Override
  46.             public void onClick(View v) {
  47.  
  48.                 //On click "register" text link, displays my Registration page to the user
  49.                 Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
  50.                 LoginActivity.this.startActivity(registerIntent);
  51.             }
  52.         });
  53.  
  54.         //create a setOnClickListener for the "sign in" button
  55.         btnSignIn.setOnClickListener(new View.OnClickListener() {
  56.  
  57.             //implementation for setOnClickListeners , onClick method
  58.             //on click ("sign in") button stores username and password values from my View into variables
  59.             //these values are sent to my server to be verified to gain access to my application
  60.             @Override
  61.             public void onClick(View v) {
  62.  
  63.                 //declare and initialise username and password variables to store user input
  64.                 final String username = etUsername.getText().toString();
  65.                 final String password = etPassword.getText().toString();
  66.  
  67.                 //declare and initialise a responseListener to store the response received from my web server
  68.                 Response.Listener<String> responseListener = new Response.Listener<String>() {
  69.  
  70.                     //implement onResponse() from Volley Library to handle servers response
  71.                     @Override
  72.                     //@param String response is the value received from my Login.php script sent by my web server
  73.                     public void onResponse(String response) {
  74.  
  75.                         //Try Catch block used to prevent app crashes if my Login.php script does not respond with a JSON String
  76.                         try {
  77.  
  78.                             //Login.php script responds to my application using JSON so my String response parameter
  79.                             //above must be converted into a JSONObject to process my web servers response.
  80.                             JSONObject serverJSONResponse = new JSONObject(response);
  81.  
  82.                             //declare and initialise variable of type Boolean to verify if the response received
  83.                             //from my web server was successful.
  84.                             boolean loginSuccessful = serverJSONResponse.getBoolean("success");
  85.  
  86.                             //if login was successful display UserAreaActivity page
  87.                             //the name of the user is passed to the UserAreaActivity page to be displayed
  88.                             if (loginSuccessful) {
  89.                                 //get and store the users profile name returned from the web server
  90.                                 String name = serverJSONResponse.getString("name");
  91.  
  92.                                 //create an intent to display the UserAreaActivity page
  93.                                 Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
  94.  
  95.                                 //send the users profile name to UserAreaActivity page
  96.                                 intent.putExtra("name", name);
  97.  
  98.                                 //display the UserAreaActivity page
  99.                                 LoginActivity.this.startActivity(intent);
  100.                             }
  101.                             else
  102.                             //if login unsuccessful, display dialog box with retry button asking user to try logging in again
  103.                             {
  104.                                 AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
  105.                                 builder.setMessage("Login Error \n\n" + "Please try logging in again" )
  106.                                         .setNegativeButton("Retry", null)
  107.                                         .create()
  108.                                         .show();
  109.                             }
  110.                         //print exception error message to identify error
  111.                         } catch (JSONException e) {
  112.                             e.printStackTrace();
  113.                         }
  114.                     }
  115.                 };
  116.  
  117.  
  118.                 //declare and initialise a new request for user to login to their user account.
  119.                 //user must provide username and password to attempt to login as they are passed to the webserver
  120.                 //responseListener is required here to receive the servers response i.e. pass / fail.
  121.                 LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
  122.  
  123.                 //declare and initialise a login queue to handle simultaneous login requests
  124.                 RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
  125.  
  126.                 //add a login request to the queue to be processed
  127.                 queue.add(loginRequest);
  128.             }
  129.         });
  130.     }
  131. }
Add Comment
Please, Sign In to add comment