Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- php #안드로이드 #로그인&회원가입
- 안드로이드 타이머 #타이머 #handler timer #android handler timer #countdowntimer
- android #tip #timer #handler #sendmessagedelayed
- db 프로시저 권한 부여 #db procedure 권한 부여 #
- Apache #PHP #마리아DB #아파치
- 터미널 #terminal #aws 인스턴스 접속
- XAMPP #PHP #mysql #Android #연동
- 안드로이드 앱 아이콘 변경 #안드로이드 앱 아이콘 사이즈 #안드로이드 앱 아이콘 가이드
- Android #bottom navigation View #Bottom tab #FRAGMENT #JAVA #Material Design #안드로이드 프래그먼트 바텀 네비게이션 뷰 커스텀 #안드로이드 메뉴 아이콘 커스텀 #안드로이드 메뉴 #프래그먼트 메뉴 #fragment menu i
- react #react 프로젝트 생성 #초보 #쉽게 #react 세팅 #react frontend #리액트 기본구조
- hedisql #mariadb #mysql #마리아db #암호화 복호화 #aes512 #aes_encrypt #aes_decrypt #hedisql 펑션 생성 #hedisql 프로시저 생성 #hedisql 매개변수 #db 펑션 매개변수 #db 프로시저 #db 함수 #db return
- 안드로이드 #앱 아이콘 #앱 아이콘 커스텀하기 #앱 로고 만들기 #앱 아이콘 만들기 #android logo custom #android icon custom #android logo 만들기 #image asset #
- 회원가입 구현 #java
- MySQL #안드로이드 로그인기능 #안드로이드 php 오류 #안드로이드 db #
- my.ini위치 확인
- mysql #dataloongforcolumn #ER_DATA_TOO_LONG #mariaDB #my.ini찾기 #숨김my.ini찾기 #strict mode 해제 #strict mode
- 부트스트랩 추가시 오류 #
- 리눅스 #우분투 포트포워딩 #리눅스 포트포워딩 #우분투22.04 php apache db #mariadb #mysql
- android hedisql #안드로이드 mariaDB #안드로이드 로그인 권한 #안드로이드 로그인
- 안드로이드 로그인 회원가입 기능 #안드로이드 php 로그인 #userValidate.php #android php 중복체그 #안드로이드 회원가입 id 중복체크 기능 #android login id check
- nodejs #express #게시판 기능 구현
- hedisql limit 1000
- MySQL(MariaDB)접속 #MariaDB원격 접속 #콘솔에서mysql접속 #원격지에서MySQL #php안드로이드 연동
- procedure does not exist
- Module not found: Error: Can't resolve '@popperjs/core'
- 안드로이드 아이콘 만들기 #안드로이드 vector asset #android vector asset custom #안드로이드 vector background color change #android pathdata editor #vector drawables edit #vector drawable background color #drawable background color #an
- 크롤링 #텍스트 크롤링 #파이썬 #구름IDE
- mariadb #mysql
- 부트스트랩 #bootstrap install #
- react #리액트 #프로젝트 초기세팅 #초기구현 #클린코드 #cleancode #node #react-node #vite #react 설치 #react 세팅 #쇼핑몰 #eslint 설정 #vite eslint #TailWindCSS란? #테일윈드적용 #TailWindCSS 적용
Archives
- Today
- Total
맨땅에 코딩
[Android] 로그인&회원가입 기능 구현 - mysql, php(외부 서버) 활용 본문
목차
반응형
실행 이미지
저는 외부 서버를 포트포워딩&인바운드 규칙 설정을 통해 열어놨습니다 (이 글에선 방법은 생략하겠습니다)
외부서버에서 HediSQL을 통해 localhostDB 127.0.0.1에 user database를 만들고 table도 user로 만들어줬습니다
회원가입&로그인 기능을 할 DB를 만들어주셨으면 이제 안드로이드앱을 설계할 차례입니다
우선 필요한 권한을 추가해줍니다
<build gradle (module: app) >
implementation 'com.android.volley:volley:1.1.1'
<AndroidManiFest.xml>
<uses-permission android:name="android.permission.INTERNET" />
android:usesCleartextTraffic="true"
전체 소스코드입니다
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginactivity">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LoginActivity"
android:usesCleartextTraffic="true">
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity" />
<activity android:name=".MainActivity"/>
</application>
</manifest>
<LoginActivity.java>
public class LoginActivity extends AppCompatActivity {
private EditText et_id, et_pass;
private Button btn_login, btn_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et_id = findViewById(R.id.et_id);
et_pass = findViewById(R.id.et_pass);
btn_login = findViewById(R.id.btn_login);
btn_register = findViewById(R.id.btn_register);
// 회원가입 버튼을 클릭 시 수행
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// EditText에 현재 입력되어있는 값을 get(가져온다)해온다.
String userID = et_id.getText().toString();
String userPass = et_pass.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// TODO : 인코딩 문제때문에 한글 DB인 경우 로그인 불가
System.out.println("hongchul" + response);
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean("success");
if (success) { // 로그인에 성공한 경우
String userID = jsonObject.getString("userID");
String userPass = jsonObject.getString("userPassword");
Toast.makeText(getApplicationContext(),"로그인에 성공하였습니다.",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("userID", userID);
intent.putExtra("userPass", userPass);
startActivity(intent);
} else { // 로그인에 실패한 경우
Toast.makeText(getApplicationContext(),"로그인에 실패하였습니다.",Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(userID, userPass, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
<LoginRequest.java>
public class LoginRequest extends StringRequest {
// 서버 URL 설정 ( PHP 파일 연동 )
final static private String URL = "http://ip입력/login.php";
private Map<String, String> map;
public LoginRequest(String userID, String userPassword, Response.Listener<String> listener) {
super(Method.POST, URL, listener, null);
map = new HashMap<>();
map.put("userID",userID);
map.put("userPassword", userPassword);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
저는 ip 입력 부분에 DB를 설계한 외부 ip를 입력해줬습니다
<MainAvtivity.java>
public class MainActivity extends AppCompatActivity {
private TextView tv_id, tv_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_id = findViewById(R.id.tv_id);
tv_pass = findViewById(R.id.tv_pass);
Intent intent = getIntent();
String userID = intent.getStringExtra("userID");
String userPass = intent.getStringExtra("userPass");
tv_id.setText(userID);
tv_pass.setText(userPass);
}
}
<RegisterActivity.java>
public class RegisterActivity extends AppCompatActivity {
private EditText et_id, et_pass, et_name, et_age;
private Button btn_register;
@Override
protected void onCreate(Bundle savedInstanceState) { // 액티비티 시작시 처음으로 실행되는 생명주기!
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// 아이디 값 찾아주기
et_id = findViewById(R.id.et_id);
et_pass = findViewById(R.id.et_pass);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
// 회원가입 버튼 클릭 시 수행
btn_register = findViewById(R.id.btn_register);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// EditText에 현재 입력되어있는 값을 get(가져온다)해온다.
String userID = et_id.getText().toString();
String userPass = et_pass.getText().toString();
String userName = et_name.getText().toString();
int userAge = Integer.parseInt(et_age.getText().toString());
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean("success");
if (success) { // 회원등록에 성공한 경우
Toast.makeText(getApplicationContext(),"회원 등록에 성공하였습니다.",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
} else { // 회원등록에 실패한 경우
Toast.makeText(getApplicationContext(),"회원 등록에 실패하였습니다.",Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
// 서버로 Volley를 이용해서 요청을 함.
RegisterRequest registerRequest = new RegisterRequest(userID,userPass,userName,userAge, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
<RegisterRequest.java>
public class RegisterRequest extends StringRequest {
// 서버 URL 설정 ( PHP 파일 연동 )
final static private String URL = "http://ip입력/Register.php";
private Map<String, String> map;
public RegisterRequest(String userID, String userPassword, String userName, int userAge, Response.Listener<String> listener) {
super(Method.POST, URL, listener, null);
map = new HashMap<>();
map.put("userID",userID);
map.put("userPassword", userPassword);
map.put("userName", userName);
map.put("userAge", userAge + "");
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
<Activity_login.xml>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<EditText
android:id="@+id/et_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="200dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="아이디"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="비밀번호"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/et_id"
app:layout_constraintStart_toStartOf="@+id/et_id"
app:layout_constraintTop_toBottomOf="@+id/et_id" />
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="로그인"
app:layout_constraintEnd_toEndOf="@+id/et_pass"
app:layout_constraintStart_toStartOf="@+id/et_pass"
app:layout_constraintTop_toBottomOf="@+id/et_pass" />
<Button
android:id="@+id/btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="회원가입"
app:layout_constraintEnd_toEndOf="@+id/btn_login"
app:layout_constraintStart_toStartOf="@+id/btn_login"
app:layout_constraintTop_toBottomOf="@+id/btn_login" />
</androidx.constraintlayout.widget.ConstraintLayout>
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Hello World!"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="아이디"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="비밀번호"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_id" />
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
<activity_register.xml>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<EditText
android:id="@+id/et_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="200dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="아이디"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="비밀번호"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/et_id"
app:layout_constraintStart_toStartOf="@+id/et_id"
app:layout_constraintTop_toBottomOf="@+id/et_id" />
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="이름"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/et_pass"
app:layout_constraintStart_toStartOf="@+id/et_pass"
app:layout_constraintTop_toBottomOf="@+id/et_pass" />
<EditText
android:id="@+id/et_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="나이"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/et_name"
app:layout_constraintStart_toStartOf="@+id/et_name"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<Button
android:id="@+id/btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="회원가입"
app:layout_constraintEnd_toEndOf="@+id/et_age"
app:layout_constraintStart_toStartOf="@+id/et_age"
app:layout_constraintTop_toBottomOf="@+id/et_age" />
</androidx.constraintlayout.widget.ConstraintLayout>
php 부분입니다
<login.php>
<?php
$con = mysqli_connect("127.0.0.1:3310", "root", "1233", "user");
mysqli_query($con,'SET NAMES utf8');
$userID = isset($_POST["userID"]) ? $_POST["userID"] : "";
$userPassword = isset($_POST["userPassword"]) ? $_POST["userPassword"] : "";
$statement = mysqli_prepare($con, "SELECT * 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, $userPassword, $userName, $userAge);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)) {
$response["success"] = true;
$response["userID"] = $userID;
$response["userPassword"] = $userPassword;
$response["userName"] = $userName;
$response["userAge"] = $userAge;
}
echo json_encode($response);
?>
<Register.php>
<?php
$con = mysqli_connect("127.0.0.1:3310", "root", "1233", "user");
mysqli_query($con,'SET NAMES utf8');
$userID = isset($_POST["userID"]) ? $_POST["userID"] : "";
$userPassword = isset($_POST["userPassword"]) ? $_POST["userPassword"] : "";
$userName = isset($_POST["userName"]) ? $_POST["userName"] : "";
$userAge = isset($_POST["userAge"]) ? $_POST["userAge"] : "";
$statement = mysqli_prepare($con, "INSERT INTO USER VALUES (?,?,?,?)");
mysqli_stmt_bind_param($statement, "sssi", $userID, $userPassword, $userName, $userAge);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
궁금한 점 있으시면 댓글 남겨주세요
감사합니다
참고 홍드로이드 유튜브: https://www.youtube.com/watch?v=ktjJ8xtt2Hg&t=2998s
반응형
'앱 개발 > Java' 카테고리의 다른 글
[Android] 로그인&회원가입 기능(2) - 중복체크(php, 프로젝트 소스코드 포함) (0) | 2022.12.23 |
---|---|
[Android] Vector Asset drawable 크기 조절 및 배경색 그리기 (0) | 2022.11.29 |
[Android] 앱 아이콘(로고) 만들기 + 변경하기 (Image Asset 사용) (0) | 2022.11.24 |
[Android] Timer 구현 - Handler (0) | 2022.09.13 |
[안드로이드] fragment 창 생성 + bottom navigation bar 커스텀 + 메뉴 아이콘 커스텀(저작권 상관X) (0) | 2022.08.10 |