Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package login.hfad.com;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by dipak on 24/03/2017.
- * This class is responsible for handling the login functionality of my application.
- * It contains implementation for the "Login" button's event handler that sends users login details
- * to my web server to be verified. It also contains error handling to ensure that all required fields
- * contain a value before attempting to log in to my system. It makes use of Volley library
- * functions to send and receive data to and from my web server.
- */
- public class LoginActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- /* Declaration and initialisation of final variables that are
- assigned a View from my activity_login XML layout file. */
- final EditText etUsername = (EditText) findViewById(R.id.etUsername);
- final EditText etPassword = (EditText) findViewById(R.id.etPassword);
- final Button btnSignIn = (Button) findViewById(R.id.bLogin);
- final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
- //create a setOnClickListener for the "register" text link
- registerLink.setOnClickListener(new View.OnClickListener(){
- //implementation for setOnClickListeners , onClick method
- @Override
- public void onClick(View v) {
- //On click "register" text link, displays my Registration page to the user
- Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
- LoginActivity.this.startActivity(registerIntent);
- }
- });
- //create a setOnClickListener for the "sign in" button
- btnSignIn.setOnClickListener(new View.OnClickListener() {
- //implementation for setOnClickListeners , onClick method
- //on click ("sign in") button stores username and password values from my View into variables
- //these values are sent to my server to be verified to gain access to my application
- @Override
- public void onClick(View v) {
- //declare and initialise username and password variables to store user input
- final String username = etUsername.getText().toString();
- final String password = etPassword.getText().toString();
- //declare and initialise a responseListener to store the response received from my web server
- Response.Listener<String> responseListener = new Response.Listener<String>() {
- //implement onResponse() from Volley Library to handle servers response
- @Override
- //@param String response is the value received from my Login.php script sent by my web server
- public void onResponse(String response) {
- //Try Catch block used to prevent app crashes if my Login.php script does not respond with a JSON String
- try {
- //Login.php script responds to my application using JSON so my String response parameter
- //above must be converted into a JSONObject to process my web servers response.
- JSONObject serverJSONResponse = new JSONObject(response);
- //declare and initialise variable of type Boolean to verify if the response received
- //from my web server was successful.
- boolean loginSuccessful = serverJSONResponse.getBoolean("success");
- //if login was successful display UserAreaActivity page
- //the name of the user is passed to the UserAreaActivity page to be displayed
- if (loginSuccessful) {
- //get and store the users profile name returned from the web server
- String name = serverJSONResponse.getString("name");
- //create an intent to display the UserAreaActivity page
- Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
- //send the users profile name to UserAreaActivity page
- intent.putExtra("name", name);
- //display the UserAreaActivity page
- LoginActivity.this.startActivity(intent);
- }
- else
- //if login unsuccessful, display dialog box with retry button asking user to try logging in again
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
- builder.setMessage("Login Error \n\n" + "Please try logging in again" )
- .setNegativeButton("Retry", null)
- .create()
- .show();
- }
- //print exception error message to identify error
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- };
- //declare and initialise a new request for user to login to their user account.
- //user must provide username and password to attempt to login as they are passed to the webserver
- //responseListener is required here to receive the servers response i.e. pass / fail.
- LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
- //declare and initialise a login queue to handle simultaneous login requests
- RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
- //add a login request to the queue to be processed
- queue.add(loginRequest);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment