[ 회원테이블 - test_member ]
필자는 mysql, php 과 교신하여 안드로이드 예제앱을 만들어 보았다.
[ AndroidManifest.xml ]
<uses-permission android:name="android.permission.INTERNET"/> 추가하자.
[ build.gradle ] (앱 수준)
1
2
3
4
|
dependencies {
.....
implementation 'com.android.volley:volley:1.1.0'
}
|
cs |
위의 사진을 보면 MainActivity 에서 최초 로그인과 회원가입 버튼이 처음에 나타나게 되고,
로그인 화면 LoginActivity 와 로그인 POST 웹요청응답 처리를 하는 LoginRequest 클래스와
회원가입 화면 RegisterActivity 와 회원가입 POST 웹요청응답 처리를 하는 RegisterRequest 클래스로 어우러지게 하였다.
[ 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
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="회원가입 및 로그인 테스트"
android:textSize="30dp"/>
<FrameLayout
android:id="@+id/frame1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/logoff_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:orientation="vertical">
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="로그인"
android:textSize="20dp"/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="회원가입"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/logon_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:orientation="vertical">
<TextView
android:id="@+id/txt_login"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_logout"
android:text="로그아웃"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
|
cs |
위의 소스에서는 25라인, 46라인처럼 logoff_layout, logon_layout 으로 각각 로그인상태, 로그아웃상태에 따라서 구분되도록 구성하였다.
[ 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
|
<?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">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:gravity="center"
android:text="회원가입"
android:textSize="40dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/register_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="아이디"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/register_et_pw"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="비밀번호"
app:layout_constraintTop_toBottomOf="@+id/register_et_id"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/register_et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="이름"
app:layout_constraintTop_toBottomOf="@+id/register_et_pw"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/register_et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:inputType="number"
android:hint="나이"
app:layout_constraintTop_toBottomOf="@+id/register_et_name"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/register_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Register"
android:textSize="38dp"
app:layout_constraintTop_toBottomOf="@+id/register_et_age"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
cs |
[ json_register_test_member.php ] - post방식으로 회원가입 응답결과처리용 php파일
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
|
<?
header("Content-Type:application/json");
include "../inc/config_noHeader.php";
$arr = array();
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$user_id = trim($_POST[user_id]);
$user_pw = trim($_POST[user_pw]);
$user_name = trim($_POST[user_name]);
$age = trim($_POST[age]);
$user_id = addslashes($user_id);
$user_pw = addslashes($user_pw);
$user_name = addslashes($user_name);
$user_age = addslashes($user_age);
$reg_date = time();
$query = "insert into test_member (user_id,user_pw,user_name,age,reg_date) values ('$user_id','$user_pw','$user_name','$user_age','$reg_date')";
if (mysql_query($query)) {
$arr["success"] = "1";
} else {
$arr["success"] = "-1";
}
} else {
$arr["success"] = "error";
}
echo json_encode($arr,JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE);
?>
|
cs |
[ RegisterRequest.java ] - POST 방식으로 웹요청할 회원가입 클래스
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 RegisterRequest extends StringRequest {
private final static String URL = "http://도메인주소/json/json_register_test_member.php";
private Map<String, String> map;
public RegisterRequest(String user_id, String user_pw, String user_name, int user_age, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(Method.POST,URL, listener, errorListener);
map = new HashMap<>();
map.put("user_id",user_id);
map.put("user_pw",user_pw);
map.put("user_name",user_name);
map.put("user_age",Integer.toString(user_age));
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
|
cs |
위 소스에서 24라인의 getParams() 가 POST 방식으로 보내질 매개변수들을 해시맵형태로 모아서 보내준다.
[ 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
74
75
76
77
78
79
80
81
82
83
|
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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.VolleyError;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class RegisterActivity extends AppCompatActivity {
private EditText register_et_id,register_et_pw,register_et_name,register_et_age;
private Button register_btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
register_et_id = findViewById(R.id.register_et_id);
register_et_pw = findViewById(R.id.register_et_pw);
register_et_name = findViewById(R.id.register_et_name);
register_et_age = findViewById(R.id.register_et_age);
register_btn1 = findViewById(R.id.register_btn1);
register_btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});
}
private void register() {
String user_id = register_et_id.getText().toString();
String user_pw = register_et_pw.getText().toString();
String user_name = register_et_name.getText().toString();
int user_age = Integer.parseInt(register_et_age.getText().toString());
Response.Listener<String> resposneListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success != null && success.equals("1")) { // 회원가입 완료
Toast.makeText(getApplicationContext(),"회원가입 성공!",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(),"회원가입 실패!",Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"회원가입 처리시 에러발생!",Toast.LENGTH_SHORT).show();
return;
}
};
// Volley 로 회원양식 웹으로 전송
RegisterRequest registerRequest = new RegisterRequest(user_id,user_pw,user_name,user_age,resposneListener,errorListener);
registerRequest.setShouldCache(false);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(registerRequest);
}
}
|
cs |
51라인부터 보면 응답성공시에 나오는 부분이다.
json_register_test_member.php 에 회원가입관련 정보를 POST방식으로 보내면 그 응답결과를
{"success":"1"} 이나 {"success":"error"} 등의 json 결과물을 받을수 있다.
이 값을 JSONObject의 객체형태로 변환시켜 받아 볼수 있다. 추가로 대괄호 형태의 json 결과물은 JSONArray 형태로 받으면 된다.
[ 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
61
62
63
64
65
66
67
68
69
70
71
72
|
<?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">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="132dp"
android:gravity="center"
android:text="로그인"
android:textSize="40dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_editor_absoluteX="-16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="아이디 : "
android:textSize="30dp" />
<EditText
android:id="@+id/login_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:layout_editor_absoluteX="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="비밀번호 : "
android:textSize="30dp" />
<EditText
android:id="@+id/login_et_pw"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/login_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Log in"
android:textSize="28dp"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
cs |
[ json_login_test_member.php ] - post방식으로 로그인 응답결과처리용 php파일
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
|
<?
header("Content-Type:application/json");
include "../inc/config_noHeader.php";
$arr = array();
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$user_id = trim($_POST[user_id]);
$user_pw = trim($_POST[user_pw]);
$user_id = addslashes($user_id);
$user_pw = addslashes($user_pw);
$query = "select user_id from test_member where user_id='$user_id' and user_pw='$user_pw'";
$res = mysql_query($query);
$row = mysql_fetch_array($res);
if ($row[user_id]) {
$arr["success"] = "1"; // 로그인 성공
} else {
$arr["success"] = "-1"; // 로그인 실패
}
} else {
$arr["success"] = "error";
}
echo json_encode($arr,JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE);
?>
|
cs |
[ LoginRequest.java ] - POST 방식으로 웹요청할 로그인 클래스
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
|
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 {
private final static String URL = "http://도메인주소/json/json_login_test_member.php";
private Map<String,String> map;
public LoginRequest(String user_id, String user_pw, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(Method.POST, URL, listener, errorListener);
map = new HashMap<>();
map.put("user_id",user_id);
map.put("user_pw",user_pw);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
|
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
|
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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.VolleyError;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity {
private EditText login_et_id,login_et_pw;
private Button login_btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setTitle("로그인");
login_et_id = findViewById(R.id.login_et_id);
login_et_pw = findViewById(R.id.login_et_pw);
login_btn1 = findViewById(R.id.login_btn1);
login_btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void login() {
final String user_id = login_et_id.getText().toString();
String user_pw = login_et_pw.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success != null && success.equals("1")) {
Toast.makeText(getApplicationContext(),"로그인 성공!",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
intent.putExtra("user_id",user_id);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(),"로그인 실패!",Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"로그인 처리시 에러발생!",Toast.LENGTH_SHORT).show();
return;
}
};
// Volley 로 로그인 양식 웹전송
LoginRequest loginRequest = new LoginRequest(user_id,user_pw,responseListener,errorListener);
loginRequest.setShouldCache(false);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
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
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
86
87
88
89
90
91
92
93
94
95
96
97
|
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LinearLayout logoff_layout, logon_layout;
Button btn_login, btn_register, btn_logout;
TextView txt_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("로그인,회원가입 테스트");
logoff_layout = findViewById(R.id.logoff_layout);
logon_layout = findViewById(R.id.logon_layout);
btn_login = findViewById(R.id.btn_login);
btn_logout = findViewById(R.id.btn_logout);
btn_register = findViewById(R.id.btn_register);
txt_login = findViewById(R.id.txt_login);
Intent intent = getIntent();
String user_id = intent.getStringExtra("user_id");
if (user_id==null) { // 로그아웃 상태
logon_layout.setVisibility(View.INVISIBLE);
logoff_layout.setVisibility(View.VISIBLE);
} else { // 로그인 상태
logon_layout.setVisibility(View.VISIBLE);
logoff_layout.setVisibility(View.INVISIBLE);
txt_login.setText(user_id + " 회원님");
}
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
btn_logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});
}
private void login() {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
private void logout() {
new AlertDialog.Builder(this)
.setTitle("로그아웃").setMessage("로그아웃 하시겠습니까?")
.setPositiveButton("로그아웃", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 로그아웃 처리
txt_login.setText("");
logon_layout.setVisibility(View.INVISIBLE);
logoff_layout.setVisibility(View.VISIBLE);
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
})
.setNegativeButton("취소", null)
.show();
}
private void register() {
Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
startActivity(intent);
}
}
|
cs |