Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.umapp;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class MainActivity extends AppCompatActivity {
- // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
- public static final int CONNECTION_TIMEOUT=10000;
- public static final int READ_TIMEOUT=15000;
- private EditText etUsername;
- private EditText etPassword;
- public String loginu;
- char AdminAuthChar='A';
- char InstAuthChar='I';
- char StudentAuthChar='S';
- char AuthFirstChar;
- public static final String un="";
- public static final String psswd="";
- SharedPreferences sharedpreferences;
- String username = "";
- String password = "";
- public static final String MyPREFERENCES = "MyPrefs" ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etUsername = (EditText) findViewById(R.id.username);
- etPassword = (EditText) findViewById(R.id.password);
- sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
- }
- // Triggers when LOGIN Button clicked
- public void checkLogin(View arg0) {
- // Get text from username and password field
- username = etUsername.getText().toString();
- password = etPassword.getText().toString();
- // Initialize AsyncLogin() class with username and password
- new AsyncLogin().execute(username,password);
- AuthFirstChar=username.charAt(0);
- }
- private class AsyncLogin extends AsyncTask<String, String, String>
- {
- ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- //this method will be running on UI thread
- pdLoading.setMessage("\tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- // Enter URL address where your php file resides
- url = new URL("http://146.185.173.181/login.inc.php");
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return "exception";
- }
- try {
- // Setup HttpURLConnection class to send and receive data from php and mysql
- conn = (HttpURLConnection)url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("POST");
- // setDoInput and setDoOutput method depict handling of both send and receive
- conn.setDoInput(true);
- conn.setDoOutput(true);
- // Append parameters to URL
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("username", params[0])
- .appendQueryParameter("password", params[1]);
- String query = builder.build().getEncodedQuery();
- // Open connection for sending data
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(query);
- writer.flush();
- writer.close();
- os.close();
- conn.connect();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- return "exception";
- }
- try {
- int response_code = conn.getResponseCode();
- // Check if successful connection made
- if (response_code == HttpURLConnection.HTTP_OK) {
- // Read data sent from server
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- // Pass data to onPostExecute method
- return(result.toString());
- }else{
- return("unsuccessful");
- }
- } catch (IOException e) {
- e.printStackTrace();
- return "exception";
- } finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result) {
- //this method will be running on UI thread
- pdLoading.dismiss();
- /* Here launching another activity when login successful. If you persist login state
- use sharedPreferences of Android. and logout button to clear sharedPreferences.
- */
- if(result.equalsIgnoreCase("true")&&AuthFirstChar==AdminAuthChar) {
- SharedPreferences.Editor editor = sharedpreferences.edit();
- Intent intent = new Intent(MainActivity.this, AdminPanel.class);
- etUsername = (EditText) findViewById(R.id.username);
- String username = etUsername.getText().toString();
- intent.putExtra("EXTRA_Username", username);
- editor.putString(un, username);
- editor.putString(psswd, password);
- editor.commit();
- startActivity(intent);
- MainActivity.this.finish();
- }
- else
- if(result.equalsIgnoreCase("true")&&AuthFirstChar==InstAuthChar) {
- SharedPreferences.Editor editor = sharedpreferences.edit();
- etUsername = (EditText) findViewById(R.id.username);
- String username = etUsername.getText().toString();
- Intent intent = new Intent(MainActivity.this, InstructorPanel.class);
- intent.putExtra("EXTRA_Username", username);
- editor.putString(un, username);
- editor.putString(psswd, password);
- editor.commit();
- startActivity(intent);
- MainActivity.this.finish();
- }
- else
- if(result.equalsIgnoreCase("true")&&AuthFirstChar==StudentAuthChar) {
- SharedPreferences.Editor editor = sharedpreferences.edit();
- Intent intent = new Intent(MainActivity.this, StudentPanel.class);
- etUsername = (EditText) findViewById(R.id.username);
- String username = etUsername.getText().toString();
- intent.putExtra("EXTRA_Username", username);
- startActivity(intent);
- editor.putString(un, username);
- editor.putString(psswd, password);
- editor.commit();
- MainActivity.this.finish();
- }
- else if (result.equalsIgnoreCase("false")){
- // If username and password does not match display a error message
- Toast.makeText(MainActivity.this, "Invalid Username or password", Toast.LENGTH_LONG);
- } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
- Toast.makeText(MainActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement