Linux(Ubuntu) Database Server with PHP For Android App - 안드로이드 앱을 위한 리눅스(우분투) 데이터베이스 서버
Android 2022. 2. 9. 17:33 |안드로이드 앱을 위한 리눅스(우분투) 데이터베이스(MySQL/MariaDB) 서버를 만들어 보자.
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
|
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = editText1.getText().toString();
String name = editText2.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
RequestData(id, name);
}
}).start();
}
});
}
public void RequestData(String id, String name) {
try {
BufferedReader bufferedReader;
StringBuilder stringBuilder = new StringBuilder();
String urlString = "http://192.168.171.200/data_check.php";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn != null) {
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String parameter = "id=" + id + "&name=" + name;
OutputStream outputStream = conn.getOutputStream();
outputStream.write(parameter.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
textView.setText(stringBuilder);
bufferedReader.close();
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
서버에 데이터를 요청하고 표시하는 소스를 작성한다. (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
|
<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_password = "1234";
$mysql_db = "member_db";
$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_db);
if (!$conn) {
die("Database Connect Error: " . mysqli_connect_error());
}
$android = strpos($_SERVER['HTTP_USER_AGENT'], "Android");
if ((($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['submit'])) || $android)
{
$post_id = trim($_POST['id']);
$post_name = trim($_POST['name']);
$sql = "SELECT * FROM member WHERE id='$post_id' AND name='$post_name'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1 ) {
$member = mysqli_fetch_assoc($result);
echo "ID: " . $member['id'] . " Name: " . $member['name'] . " Age: " . $member['age'];
} else {
echo "No Data.";
mysqli_close($conn);
exit;
}
} else {
echo "No Android Device Detected.";
}
mysqli_close($conn);
?>
|
아파치 서버 디렉토리에 안드로이드 앱 요청을 처리할 PHP 파일을 작성한다. (/var/www/html/data_check.php)
※ 참고
'Android' 카테고리의 다른 글
User Interface Manipulation From UI Thread - 다른 스레드에서 UI 객체 접근하기 (0) | 2022.02.13 |
---|---|
Google Android Maps SDK 구글 안드로이드 맵 (0) | 2022.02.12 |
Naver Reverse Geocoding For Android 안드로이드 네이버 리버스 지오코딩 (0) | 2022.02.06 |
Naver Geocoding For Android 안드로이드 네이버 지오코딩 (1) | 2022.02.05 |
Naver Mobile Dynamic Map For Android 안드로이드 네이버 모바일 다이나믹 맵 2 (0) | 2022.02.05 |