AndroidManifest.xml
//uses-permission internet.
//cleartextTraffic true 설정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datatransfer">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true"
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">
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<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/ic_launcher_foreground"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hohohoho"
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="register help"
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/userId"
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/userPwd"
android:drawableStart="@drawable/ic_https_black_24dp"
android:drawableLeft="@drawable/ic_https_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/colorPrimaryDark"
android:text="login"
android:id="@+id/loginBtn"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18dp"
android:layout_marginTop="10dp"
android:text="register"
android:id="@+id/registerBtn"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="register"
android:textColor="#ffffff"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"/>
</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/userId"
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/userPwd"
android:drawableStart="@drawable/ic_https_black_24dp"
android:drawableLeft="@drawable/ic_https_black_24dp"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"/>
<EditText
android:layout_width="280dp"
android:layout_height="wrap_content"
android:hint="email"
android:layout_gravity="center"
android:padding="10dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:id="@+id/userEmail"
android:drawableStart="@drawable/ic_email_black_24dp"
android:drawableLeft="@drawable/ic_email_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/colorPrimaryDark"
android:text="register"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:id="@+id/registerBtn"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
LoginActivity.java
//URL url = new URL("http://www.uccstudent.com/zLogin.jsp");
package com.example.datatransfer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class LoginActivity extends AppCompatActivity {
EditText userId, userPwd;
Button loginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userId = (EditText) findViewById(R.id.userId);
userPwd = (EditText) findViewById(R.id.userPwd);
loginBtn = (Button) findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(btnListener);
//register page로 이동
TextView registerButton = (TextView) findViewById(R.id.registerBtn);
registerButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
}
class CustomTask extends AsyncTask<String, Void, String>{
String sendMsg, receiveMsg;
@Override
protected String doInBackground(String... strings){
try{
String str;
URL url = new URL("http://10.241.131.162:8080/design/zLogin.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
sendMsg = "userId="+strings[0]+"&userPwd="+strings[1];
osw.write(sendMsg);
osw.flush();
if(conn.getResponseCode() == conn.HTTP_OK){
InputStreamReader tmp = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(tmp);
StringBuffer buffer = new StringBuffer();
while ((str = reader.readLine()) != null) {
buffer.append(str);
}
receiveMsg = buffer.toString();
} else {
Log.i("result", conn.getResponseCode()+"error");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return receiveMsg;
}
}
View.OnClickListener btnListener = new View.OnClickListener(){
@Override
public void onClick(View view){
switch(view.getId()){
case R.id.loginBtn:
String loginId = userId.getText().toString();
String loginPwd = userPwd.getText().toString();
try{
String result = new CustomTask().execute(loginId, loginPwd).get();
if(result.equals("true")){
Toast.makeText(LoginActivity.this,"login", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}else if(result.equals("false")){
Toast.makeText(LoginActivity.this,"wrong ID or Password",Toast.LENGTH_SHORT).show();
userId.setText("");
userPwd.setText("");
}else if(result.equals("noId")){
Toast.makeText(LoginActivity.this,"it doesn't exist",Toast.LENGTH_SHORT).show();
userId.setText("");
userPwd.setText("");
}
}catch(Exception e){
e.printStackTrace();
}break;
}
}
};
}
RegisterActivity.java
//URL url = new URL("http://www.uccstudent.com/zRegister.jsp");
package com.example.datatransfer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class RegisterActivity extends AppCompatActivity {
EditText userId, userPwd, userEmail;
Button registerBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
userId = (EditText) findViewById(R.id.userId);
userPwd = (EditText) findViewById(R.id.userPwd);
userEmail = (EditText) findViewById(R.id.userEmail);
registerBtn = (Button) findViewById(R.id.registerBtn);
registerBtn.setOnClickListener(btnListener);
}
//연결과 data전송을 위해
class CustomTask extends AsyncTask<String, Void, String>{
String sendMsg, receiveMsg;
@Override
protected String doInBackground(String... strings){
try{
String str;
URL url = new URL("http://10.241.131.162:8080/design/zRegister.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
sendMsg = "userId="+strings[0]+"&userPwd="+strings[1]+"&userEmail="+strings[2];
osw.write(sendMsg);
osw.flush();
if(conn.getResponseCode() == conn.HTTP_OK){
InputStreamReader tmp = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(tmp);
StringBuffer buffer = new StringBuffer();
while ((str = reader.readLine()) != null) {
buffer.append(str);
}
receiveMsg = buffer.toString();
} else {
Log.i("result", conn.getResponseCode()+"error");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return receiveMsg;
}
}
//버튼 클릭시
View.OnClickListener btnListener = new View.OnClickListener(){
@Override
public void onClick(View view){
switch(view.getId()){
case R.id.registerBtn:
String joinId = userId.getText().toString();
String joinPwd = userPwd.getText().toString();
String joinEmail = userEmail.getText().toString();
try{
String result = new CustomTask().execute(joinId, joinPwd, joinEmail).get();
if(result.equals("userId")){
Toast.makeText(RegisterActivity.this,"it already exists", Toast.LENGTH_SHORT).show();
userId.setText("");
userPwd.setText("");
userEmail.setText("");
}else if(result.equals("ok")){
userId.setText("");
userPwd.setText("");
userEmail.setText("");
Toast.makeText(RegisterActivity.this,"registered successfully", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}break;
}
}
};
}
zRegister.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import = "android.*"%>
<%
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
String userEmail = request.getParameter("userEmail");
zDAO zdao = zDAO.getInstance();
String returns = zdao.insertUser(userId, userPwd, userEmail);
out.print(returns);
%>
zLogin.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import = "android.*"%>
<%
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
zDAO zdao = zDAO.getInstance();
String returns = zdao.loginUser(userId, userPwd);
out.print(returns);
%>
zDAO.java
package android;
import java.sql.*;
public class zDAO {
private static zDAO instance = new zDAO();
public static zDAO getInstance() {
return instance;
}
public zDAO() {
}
public String insertUser(String userId, String userPwd, String userEmail) {
Connection conn = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt1 = null;
ResultSet rs = null;
String returns = "";
String sql2 = "insert into test VALUES(?,?,?)";
String sql1 = "select userId from test where userId=?";
String url = "jdbc:mysql://localhost:3306/jusung1025";
String uId = "***********";
String uPw = "***********";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,uId,uPw);
pstmt1 = conn.prepareStatement(sql1);
pstmt1.setString(1, userId);
rs = pstmt1.executeQuery();
if(rs.next()) {
if(rs.getString("userId").equals(userId)){
returns = "userId";
}
}else {
pstmt2 = conn.prepareStatement(sql2);
pstmt2.setString(1, userId);
pstmt2.setString(2, userPwd);
pstmt2.setString(3, userEmail);
pstmt2.executeUpdate();
returns = "ok";
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt2 != null) pstmt2.close();
if(pstmt1 != null) pstmt1.close();
if(conn != null) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return returns;
}
public String loginUser(String userId, String userPwd) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String returns = "";
String sql = "select userId,userPwd from test where userId=?";
String url = "jdbc:mysql://localhost:3306/jusung1025";
String uId = "*******";
String uPw = "********";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,uId,uPw);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
rs = pstmt.executeQuery();
if(rs.next()) {
if(rs.getString("userId").equals(userId) && rs.getString("userPwd").equals(userPwd)) {
returns = "true";
}else {
returns = "false";
}
}else {
returns = "noId";
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return returns;
}
}
'android studio' 카테고리의 다른 글
android register db connection using JSP (0) | 2019.09.06 |
---|---|
android internet check (0) | 2019.09.05 |
activity life cycle (0) | 2019.08.14 |
intent (0) | 2019.08.14 |
startActivityForResult()(화면전환) (0) | 2019.08.13 |