프로그래밍/안드로이드

안드로이드 - 5. 수강신청앱 로그인 기능 구현

가카리 2018. 7. 23. 22:56
반응형




실행화면


 1 먼저 회원가입을 합니다.


2 회원 정보를 입력한 뒤 REGISTER 버튼을 누릅니다.


3 다음과 같이 로그인을 합니다.



4 로그인에 성공하면 다음과 같은 메인화면이 나옵니다.



UserLogin.php(180923 추가)

<?php
    $con = mysqli_connect('localhost', 'root', '1234', 'registration');
     //안드로이드 앱으로부터 아래 값들을 받음
    $userID = $_POST["userID"];
    $userPassword = $_POST["userPassword"];
    $statement = mysqli_prepare($con, "SELECT userID FROM USER WHERE userID = ? AND userPassword = ?");
    mysqli_stmt_bind_param($statement, "ss", $userID, $userPassword);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $userID);
    $response = array();
    $response["success"] = false;
    while(mysqli_stmt_fetch($statement)){
      $response["success"] = true;
      $response["userID"] = $userID;
    }
    echo json_encode($response);
?>


MainActivity.java


로그인 성공시 화면


package com.example.kch.registration_v5;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

}



activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.kch.registration_v5.MainActivity">


</android.support.constraint.ConstraintLayout>



LoginRequest.java


로그인 시도를 위한 StringRequest를 상속받는 클래스


package com.example.kch.registration_v5;


import com.android.volley.AuthFailureError;

import com.android.volley.Response;

import com.android.volley.toolbox.StringRequest;


import java.util.HashMap;

import java.util.Map;


/**

* Created by kch on 2018. 7. 23..

*/


public class LoginRequest extends StringRequest{


    final static private String URL = "http://10.0.2.2:8080/registration/UserLogin.php";

    private Map<String, String> parameters;


    public LoginRequest(String userID, String userPassword, Response.Listener<String> listener){

        super(Method.POST, URL, listener, null);//해당 URL에 POST방식으로 파마미터들을 전송함

        parameters = new HashMap<>();

        parameters.put("userID", userID);

        parameters.put("userPassword", userPassword);

    }


    @Override

    protected Map<String, String> getParams() throws AuthFailureError {

        return parameters;

    }



}




 LoginActivity.java


실제로 로그인을 위한 ID와 패스워드를 입력받고 서버로 넘겨줘서 인증하는 부분입니다.


package com.example.kch.registration_v5;


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 android.widget.Toast;


import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.toolbox.Volley;


import org.json.JSONObject;


public class LoginActivity extends AppCompatActivity {


    private AlertDialog dialog;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);


        TextView registerButton = (TextView)findViewById(R.id.registerButton);


        //버튼이 눌리면 RegisterActivity로 가게함

        registerButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);

                LoginActivity.this.startActivity(registerIntent);

            }

        });


        final EditText idText = (EditText) findViewById(R.id.idText);

        final EditText passwordText = (EditText) findViewById(R.id.passwordText);

        final Button loginButton = (Button)findViewById(R.id.loginButton);


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                String userID = idText.getText().toString();

                String userPassword = passwordText.getText().toString();


                Response.Listener<String> responseLisner = new Response.Listener<String>(){


                    @Override

                    public void onResponse(String response) {

                        try{

                            JSONObject jsonResponse = new JSONObject(response);

                            boolean success = jsonResponse.getBoolean("success");


                            if(success){

                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

                                dialog = builder.setMessage("로그인에 성공했습니다")

                                        .setPositiveButton("확인", null)

                                        .create();

                                dialog.show();

                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);

                                LoginActivity.this.startActivity(intent);

                                finish();

                            }else {

                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

                                dialog = builder.setMessage("계정을 다시 확인하세요")

                                        .setNegativeButton("다시시도", null)

                                        .create();

                                dialog.show();

                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);

                                LoginActivity.this.startActivity(intent);

                                finish();

                            }


                        }catch (Exception e){

                            e.printStackTrace();

                        }

                    }

                };


                LoginRequest loginRequest = new LoginRequest(userID, userPassword, responseLisner);

                RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);

                queue.add(loginRequest);


            }

        });


    }


    @Override

    protected void onStop(){

        super.onStop();

        if(dialog != null){//다이얼로그가 켜져있을때 함부로 종료가 되지 않게함

            dialog.dismiss();

            dialog = null;

        }

    }



}



activity_login.xml



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.example.kch.registration_v5.LoginActivity">

   

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical">

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="225dp"

            android:orientation="vertical"

            android:background="@color/colorPrimary"

            >

            <ImageView

                android:layout_width="125dp"

                android:layout_height="125dp"

                android:layout_gravity="center"

                android:layout_marginTop="20dp"

                android:src="@drawable/univlogo"

                />

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="Kyowon UNIV"

                android:textColor="#ffffff"

                android:textSize="25dp"

                android:textStyle="bold"

                android:layout_marginTop="10dp"

                />


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="Registration Helper"

                android:textColor="#ffffff"

                android:textSize="18dp"

                android:textStyle="bold"

                />

        </LinearLayout>


        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical">

           

            <EditText

                android:layout_width="280dp"

                android:layout_height="wrap_content"

                android:hint="ID"

                android:layout_gravity="center"

                android:padding="10dp"

                android:textColor="#ffffff"

                android:textColorHint="#ffffff"

                android:id="@+id/idText"

                android:drawableStart="@drawable/ic_person_black_24dp"

                android:drawableLeft="@drawable/ic_person_black_24dp"

                android:layout_marginTop="50dp"

                android:background="@color/colorPrimary"

                />


            <EditText

                android:inputType="textPassword"

                android:layout_width="280dp"

                android:layout_height="wrap_content"

                android:hint="PASSWORD"

                android:layout_gravity="center"

                android:padding="10dp"

                android:textColor="#ffffff"

                android:textColorHint="#ffffff"

                android:id="@+id/passwordText"

                android:drawableStart="@drawable/ic_lock_outline_black_24dp"

                android:drawableLeft="@drawable/ic_lock_outline_black_24dp"

                android:layout_marginTop="10dp"

                android:background="@color/colorPrimary"

                />


            <Button

                android:layout_width="280dp"

                android:layout_height="wrap_content"

                android:textSize="20dp"

                android:textStyle="bold"

                android:textColor="#ffffff"

                android:background="@color/colorPrimary"

                android:text="Login"

                android:layout_marginTop="10dp"

                android:layout_gravity="center"

                android:id="@+id/loginButton"


                />

           

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:textSize="18dp"

                android:layout_marginTop="10dp"

                android:text="Member Registration"

                android:id="@+id/registerButton"

                />



        </LinearLayout>

       

    </LinearLayout>




</LinearLayout>