Where are you?
  • My Project
  • Blog
  • About Me
  • Source Code & Downloads
    • Login.java
    • MainActivity.java
    • MapsActivity.java
    • Tab1.java
    • Tab2.java
    • Tab3.java
login.java
File Size: 6 kb
File Type: java
Download File

package c.test2;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
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 android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;

/*********************************************************************************************
 * Login Class/Activity
 * This activity takes care of the login feature in the application. emaildata and
        passworddata are grabbed from the login screen and sent to the server to
        check the validity of the user. Compares to userlist.txt file on compsco02.
 * Calls btnTest's onClickListener when button is pressed.
*********************************************************************************************/
public class Login extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Button btntest = (Button) findViewById(R.id.btnTest);
        final EditText emaildata = (EditText) findViewById(R.id.txtEmail);
        final EditText passworddata = (EditText) findViewById(R.id.txtPassword);

        /**************************************************************
         * btnTest OnClickListener
         * Upon button click, executes ConnectTask thread to contact
                userlist.txt file on compsci02 server. Sends in server
                url, emaildata, and passworddata.
        **************************************************************/
        btntest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ConnectTask().execute("http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/userlist.txt", emaildata.getText().toString(), passworddata.getText().toString());
            }
        });
    }

    /******************************************************************************************
     * ConnectTask
     * This AsyncTask uses an HttpURLConnection to contact the compsci02 server and read the
            data from the userlist.txt file.
     * Has background function and OnPostExecute function
     *****************************************************************************************/
    public class ConnectTask extends AsyncTask<String, String, String>
    {
        /**************************************************************
         * doInBackground
         * Creates a connection with the file on the server and reads
                each line one by one until the end of the file. if the
                email and password match on in the file then it will
                log that user in. Otherwise it will notify user
                appropriatly.
         **************************************************************/
        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            String user[];

            String useremail = params[1]; //grabs emaildata that was sent in through parameters
            String userpassword = params[2]; //grabs passworddata through parameters

            try {
                URL url = new URL(params[0]); //grabs URL sent in through parameters
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));

                String line = "";
                //while loop goes through every line in file
                while ((line = reader.readLine()) != null)
                {
                    user = line.split(";");
                    if(Objects.equals(user[0], useremail))
                    {
                        if(Objects.equals(user[1], userpassword))
                        {
                            return user[2];
                        }
                        return "Password does not match email";
                    }
                }
                return "User Not Found";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return "Error";
        }

        /**************************************************************
         * onPostExecute
         * the AsyncTask's onPostExecute function connects the
                independant thread with the main threads UI. aka this
                is where the data gets placed to the screen or where
                the program can start a new activity.
         * This function checks to see if what the result returned from the
                background function is, and notifys the user correctly.
                if the user was found correctly in file then it will
                start the mainactivity.
         **************************************************************/
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if(Objects.equals(result, "User Not Found"))
            {
                Toast.makeText(Login.this,result,Toast.LENGTH_SHORT).show();
            }
            else if(Objects.equals(result, "Password does not match email"))
            {
                Toast.makeText(Login.this,result,Toast.LENGTH_SHORT).show();
            }
            else if(Objects.equals(result, "Error"))
            {
                Toast.makeText(Login.this,result,Toast.LENGTH_SHORT).show();
            }
            else
            {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.putExtra("user", result); //passes variable between different activities
                startActivity(i);
            }
        }
    }
}

Create a free web site with Weebly
  • My Project
  • Blog
  • About Me
  • Source Code & Downloads
    • Login.java
    • MainActivity.java
    • MapsActivity.java
    • Tab1.java
    • Tab2.java
    • Tab3.java