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
- 안드로이드 앱 만들기
- 디자이너
- 안드로이드 튜토리얼
- 앱 만드는 법
- it커뮤니티
- android tutorial
- 미라클코딩클럽
- 개발강의
- 안드로이드 스튜디오
- 자바
- 취준생
- IOS
- 플러터
- Android
- Android Studio
- hongdroid
- 안드로이드 기초
- 기획자
- 홍드로이드
- flutter
- 창업가
- 개발자
- 안드로이드
- java
- 앱 만들기
- 대학생
- 안드로이드 서비스
- 코틀린
- 코딩
- 안드로이드 코딩 기초
Archives
- Today
- Total
홍드로이드의 야매코딩
#29 안드로이드 스튜디오 로그인&회원가입 (Login&Register) 예제 [ 홍드로이드 ] 본문
최근 androidx 라는 새로운 지원 라이브러리가 출시하여서
안드로이드 스튜디오 최신버전을 쓰는 구독자 여러분들께서
제 예제를 올바르게 구현하였는데도 빈번하게 에러가 나실 수 있습니다.
왜냐하면 최신버전 안드로이드 스튜디오 에서는 new project로 만들 시 androidx 지원 라이브러리가
기본적으로 체크박스 되어있기 때문입니다.
예제마다 오류가 발생하게되면 최대한 androidx 에 맞춰서 예제영상 고정댓글란에
에러대응법을 찾아서 적도록 하겠습니다.
2019년 즈음 이후의 여러분들께서는 저의 예제가 support 라이브러리를 이용하여 구현했기때문에
androidx 마이그레이션 방법을 공부해두시길 부탁드립니다.
for example ) import android.support.v7.app.AppCompatActivity; -> import androidx.appcompat.app.AppCompatActivity;
build.gradle dependency에 http통신 volley 라이브러리 추가
implementation 'com.android.volley:volley:1.1.1'
<AndroidManifest.xml>
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 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hongdroid.registerloginexample"> <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/AppTheme" android:usesCleartextTraffic="true"> <!-- Android 9 버전부터 이 옵션 추가해야함. http 보안관련 때문에.. --> <activity android:name=".LoginActivity"> <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"> </activity> </application> </manifest> | cs |
<activity_main.xml>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" /> </android.support.constraint.ConstraintLayout> | cs |
<activity_register.xml>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" /> </android.support.constraint.ConstraintLayout> | cs |
<activity_login.xml>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" /> </android.support.constraint.ConstraintLayout> | cs |
<RegisterRequest.java>
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 | import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.toolbox.StringRequest; import java.util.HashMap; import java.util.Map; public class RegisterRequest extends StringRequest { // 서버 URL 설정 ( PHP 파일 연동 ) final static private String URL = "http://닷홈아디.dothome.co.kr/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; } } | cs |
<LoginRequest.java>
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 | import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.toolbox.StringRequest; import java.util.HashMap; import java.util.Map; public class LoginRequest extends StringRequest { // 서버 URL 설정 ( PHP 파일 연동 ) final static private String URL = "http://닷홈아디.dothome.co.kr/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; } } | cs |
<RegisterActivity.java>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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.Toast; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; 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); } }); } } | cs |
<LoginActivity.java>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; 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); } }); } } | cs |
<MainActivity.java>
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 | import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; 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); } } | cs |
'Android Java' 카테고리의 다른 글
#31 안드로이드 스튜디오 네트워크 상태 체크 (Broadcast Receiver) 예제 [ 홍드로이드 ] (0) | 2020.06.30 |
---|---|
#30 안드로이드 스튜디오 랠러티브 레이아웃 (Relative Layout) 예제 [ 홍드로이드 ] (0) | 2020.06.30 |
#28 안드로이드 스튜디오 리니어 레이아웃 ( LinearLayout ) 예제 [ 홍드로이드 ] (1) | 2020.06.26 |
#27 안드로이드 스튜디오 버튼 클릭 효과 만들기 (Button Selector) 예제 [ 홍드로이드 ] (2) | 2019.06.08 |
#26 안드로이드 스튜디오 다음 화면으로부터 값 전달받기 (StartActivityForResult) 예제 [ 홍드로이드 ] (0) | 2019.06.08 |
Comments