오늘이라도
[Android] 30. 안드로이드 & DB & 서버(스프링) 연동 ② : 로그인 후 서브 화면 구성, 서버 연결 까지 본문
[Android] 30. 안드로이드 & DB & 서버(스프링) 연동 ② : 로그인 후 서브 화면 구성, 서버 연결 까지
upcake_ 2020. 6. 18. 12:26https://github.com/upcake/Class_Examples
교육 중에 작성한 예제들은 깃허브에 올려두고 있습니다.
gif 파일은 클릭해서 보는 것이 정확합니다.
- 안드로이드 & DB & 서버(스프링) 연동 ② : 로그인 후 서브 화면 구성, 서버 연결 까지 -
○ 작업 순서
1. MainActivity.java
- 로그인 화면 서브1 액티비티 인텐트 올리게끔 작성
2. Sub1.java
- 클래스 생성
- 객체 선언
- 실행해서 Sub1으로 화면이 넘어가는지 확인
※ Item View에 출력하는 작동 순서
- Table <=> DTO <=> ItemView(ImageView, Id, name, hiredate) 테이블에서 DTO를 꺼내와서 ItemView에 출력
3. DTO/MyItem.java : 내가 원하는 데이터 모양을 만드는 DTO
- 클래스 생성
- 직렬화
- 객체 선언
- 생성자 생성
- Getter, Setter 생성
4. SQL Developer : android 테이블 생성
5. build.gradle
- 리사이클러뷰, 디자인 라이브러리 임플리먼트
- URL 이미지 로더 Glide 임플리먼트
- 싱크
6. myitem_view xml 생성
7. Adapter 패키지 생성
8. Adapter/MyRecyclerViewAdapter.java 생성
- 객체 선언
- ItemViewHolder 생성
RecyclerView.ViewHolder 상속
객체 초기화
- setItem 메서드 작성
- RecyclerView.Adapter 상속
- 임플리먼트 메서드
- Context, ArrayList 객체 선언
9. Sub1.java
- ArrayList 객체 선언
10. Adapter/MyRecyclerViewAdapter.java
- 생성자 생성
- onCreateViewHolder()
LayoutInflater 객체 초기화
View 객체 초기화
ItemViewHolder 리턴
- onBindViewHolder()
logt, logd
Myitem 객체 초기화
화면 만들기
11. Sub1.java
- MyItem 객체 selItem 초기화
12. Adapter/MyRecyclerViewAdapter.java
- onBindViewHolder()
레이아웃 클릭 시 설정
logd, selItem, Toast
- getItemCount()
리스트 사이즈 리턴하게 설정
- removeAllItem() 메서드 작성
13. Sub1.java
- 어댑터 객체 adapter 선언
- onCreate() 블록 작성
//리사이클러 뷰 시작
14. ATask/ListSelect.java 생성
- AsyncTask 상속
- 임플리먼트 메서드 doInBackground()
- ArrayList, 어댑터, 프로그레스 다이얼로그 객체 선언
- 선언한 객체들의 생성자 생성
- LoginSelect에서 HTTP ~ doInBackground() 복사
- doInBackground()
postURL anSelectMulti로 수정
//문자열 데이터 추가 부분 삭제
AsynTask 상속 문의 엘레멘트를 void, void, string으로 변경
- onPostExecute() 메서드 생성
- readJsonStream(), readMessage() 복사 후 붙여 넣기
- doInBackground()
//응답 부분을 readJsonStream(inputStream);으로 바꿈
- onPostExecute()
adapter.notifyDataSetChagned();
- onPreExecute() 생성
15. Sub1.java
- onCreate()
if문 작성
16. 스프링
- Acontroller.java > ASelectMultiCommand.java > AnDAO.java anSelectMulti()
> ASelectMultiCommand.java 모델에 adtos를 anSelectMulti로 집어넣음
> AController.java anSelectMulti jsp로 이동 > anSelectMulti.jsp json형태로 만들어서 출력
○ 작동 화면 및 코드
- 서브 화면 연결 부분이 완성되지는 않았지만, 주소창에 직접 anSelectMulti를 호출함으로써 서버 연결이 제대로 되었는지 확인할 수 있다.
package com.example.my50_project;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
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.example.my50_project.ATask.LoginSelect;
import com.example.my50_project.DTO.MemberDTO;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
// 로그인이 성공하면 static 로그인DTO 변수에 담아서
// 어느곳에서나 접근할 수 있게 한다
public static MemberDTO loginDTO = null;
EditText etID, etPASSWD;
Button btnLogin, btnJoin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkDangerousPermissions();
etID = findViewById(R.id.etId);
etPASSWD = findViewById(R.id.etPASSWD);
btnLogin = findViewById(R.id.btnLogin);
btnJoin = findViewById(R.id.btnJoin);
//로그인 버튼
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(etID.getText().toString().length() != 0 &&
etPASSWD.getText().toString().length() != 0) { //아이디와 비밀번호의 길이가 0이 아니면
String id = etID.getText().toString();
String passwd = etPASSWD.getText().toString();
LoginSelect loginSelect = new LoginSelect(id, passwd);
try {
loginSelect.execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(loginDTO != null) { //로그인 정보가 있으면, 서브1 화면을 띄움
Log.d(TAG, "onClick: id:" + loginDTO.getId());
Intent intent = new Intent(getApplicationContext(), Sub1.class);
startActivity(intent);
} else { //로그인 정보가 맞지 않으면 토스트창 띄우고 id, pw칸 지우고 id칸에 포커스
Toast.makeText(MainActivity.this, "아이디나 비밀번호가 틀렸습니다.", Toast.LENGTH_SHORT).show();
etID.setText("");
etPASSWD.setText("");
etID.requestFocus();
}
}
}
});
//회원 가입 버튼
btnJoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), JoinActivity.class);
startActivity(intent);
}
});
}
private void checkDangerousPermissions() {
String[] permissions = {
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_WIFI_STATE
};
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (int i = 0; i < permissions.length; i++) {
permissionCheck = ContextCompat.checkSelfPermission(this, permissions[i]);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
break;
}
}
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "권한 있음", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "권한 없음", Toast.LENGTH_LONG).show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
Toast.makeText(this, "권한 설명 필요함.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this, permissions, 1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 1) {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, permissions[i] + " 권한이 승인됨.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, permissions[i] + " 권한이 승인되지 않음.", Toast.LENGTH_LONG).show();
}
}
}
}
}
▲MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_sub1"
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="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
android:onClick="btn1Clicked"
android:text="추가"
android:textSize="14sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
android:onClick="btn2Clicked"
android:text="수정" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
android:onClick="btn3Clicked"
android:text="삭제" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
android:onClick="btn4Clicked"
android:text="메인으로" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="@+id/progressBarSub1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
</FrameLayout>
</LinearLayout>
▲activity_sub1.xml
package com.example.my50_project;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.my50_project.ATask.ListSelect;
import com.example.my50_project.Adapter.MyRecyclerViewAdapter;
import com.example.my50_project.DTO.MyItem;
import java.util.ArrayList;
import static com.example.my50_project.Common.CommonMethod.isNetworkConnected;
public class Sub1 extends AppCompatActivity {
private static final String TAG = "main Sub1";
public static MyItem selItem = null;
Button button1, button2, button3, button4;
RecyclerView recyclerView;
MyRecyclerViewAdapter adapter;
ArrayList<MyItem> myItemArrayList;
ListSelect listSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub1);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
//리사이클러 뷰 시작
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
myItemArrayList = new ArrayList<>();
adapter = new MyRecyclerViewAdapter(this, myItemArrayList);
recyclerView.setAdapter(adapter);
if(isNetworkConnected(this) == true) {
listSelect = new ListSelect(myItemArrayList, adapter);
listSelect.execute();
} else {
Toast.makeText(this, "인터넷 연결 실패!", Toast.LENGTH_SHORT).show();
}
}
}
▲Sub1.java
package com.example.my50_project.ATask;
import android.app.ProgressDialog;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;
import com.example.my50_project.Adapter.MyRecyclerViewAdapter;
import com.example.my50_project.DTO.MyItem;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import static com.example.my50_project.Common.CommonMethod.ipConfig;
import static com.example.my50_project.MainActivity.loginDTO;
public class ListSelect extends AsyncTask<Void, Void, String> {
ArrayList<MyItem> myItemArrayList;
MyRecyclerViewAdapter adapter;
ProgressDialog progressDialog;
public ListSelect(ArrayList<MyItem> myItemArrayList, MyRecyclerViewAdapter adapter) {
this.myItemArrayList = myItemArrayList;
this.adapter = adapter;
}
HttpClient httpClient;
HttpPost httpPost;
HttpResponse httpResponse;
HttpEntity httpEntity;
@Override
protected void onPreExecute() {
super.onPreExecute();
//프로그레스바 보이게
}
@Override
protected String doInBackground(Void... voids) {
try {
// MultipartEntityBuild 생성
// 필수 부분
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(Charset.forName("UTF-8"));
String postURL = ipConfig + "/app/anSelectMulti";
// /app/ 뒤에는 서버의 AController에 적힌 맵핑과 동일하게 적는다.
// 전송
InputStream inputStream = null;
httpClient = AndroidHttpClient.newInstance("Android");
httpPost = new HttpPost(postURL);
httpPost.setEntity(builder.build());
httpResponse = httpClient.execute(httpPost); //여기 라인에서 DB에 보냄
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
//응답
readJsonStream(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpEntity != null) {
httpEntity = null;
}
if (httpResponse != null) {
httpResponse = null;
}
if (httpPost != null) {
httpPost = null;
}
if (httpClient != null) {
httpClient = null;
}
} //try catch finally
return "asdf";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//프로그레스바 안보이게
adapter.notifyDataSetChanged();
}
public void readJsonStream(InputStream inputStream) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
try {
reader.beginArray();
while (reader.hasNext()) {
myItemArrayList.add(readMessage(reader));
}
reader.endArray();
} finally {
reader.close();
}
}
public MyItem readMessage(JsonReader reader) throws IOException {
String id = "", name = "", hire_date = "", image_path = "";
reader.beginObject();
while (reader.hasNext()) {
String readStr = reader.nextName();
if (readStr.equals("id")) {
id = reader.nextString();
} else if (readStr.equals("name")) {
name = reader.nextString();
} else if (readStr.equals("hire_date")) {
String[] temp = reader.nextString().replace("월", "-").replace(",", "-")
.replace(" ", "").split("-");
hire_date = temp[2] + "-" + temp[0] + "-" + temp[1];
} else if (readStr.equals("image_path")) {
image_path = reader.nextString();
}else {
reader.skipValue();
}
}
reader.endObject();
Log.d("listselect:myitem", id + "," + name + "," + hire_date + "," + image_path);
return new MyItem(id, name, hire_date, image_path);
}
}
▲ListSelect.java
package com.example.my50_project.ATask;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;
import com.example.my50_project.Common.CommonMethod;
import com.example.my50_project.DTO.MemberDTO;
import static com.example.my50_project.Common.CommonMethod.ipConfig;
import static com.example.my50_project.MainActivity.loginDTO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class LoginSelect extends AsyncTask<Void, Void, String> {
private static final String TAG = "main LoginSelect";
String id, passwd;
// 데이터베이스에 삽입결과 0보다크면 삽입성공, 같거나 작으면 실패
// 필수 부분
//String state = ""; 로그인에서는 state를 안쓰기때문에 필요없다
HttpClient httpClient;
HttpPost httpPost;
HttpResponse httpResponse;
HttpEntity httpEntity;
public LoginSelect(String id, String passwd) {
this.id = id;
this.passwd = passwd;
}
@Override
protected String doInBackground(Void... voids) {
try {
// MultipartEntityBuild 생성
// 필수 부분
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(Charset.forName("UTF-8"));
// 문자열 및 데이터 추가
builder.addTextBody("id", id, ContentType.create("Multipart/related", "UTF-8"));
builder.addTextBody("passwd", passwd, ContentType.create("Multipart/related", "UTF-8"));
String postURL = ipConfig + "/app/anLogin";
// /app/ 뒤에는 서버의 AController에 적힌 맵핑과 동일하게 적는다.
// 전송
InputStream inputStream = null;
httpClient = AndroidHttpClient.newInstance("Android");
httpPost = new HttpPost(postURL);
httpPost.setEntity(builder.build());
httpResponse = httpClient.execute(httpPost); //여기 라인에서 DB에 보냄
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
//응답 : JSON형태로 받음
loginDTO = readMessage(inputStream);
Log.d(TAG, "doInBackground: " + loginDTO.getId());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(httpEntity != null){
httpEntity = null;
}
if(httpResponse != null){
httpResponse = null;
}
if(httpPost != null){
httpPost = null;
}
if(httpClient != null){
httpClient = null;
}
} //try catch finally
return "LoginDTO select complete";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d(TAG, "onPostExecute: " + result);
}
public MemberDTO readMessage(InputStream inputStream) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
String id = "", name = "", phonenumber = "", address = "";
//비밀번호는 가져올 필요가 없고, 보안을 위해 가져오지 않는다.
reader.beginObject();
while (reader.hasNext()) {
String readStr = reader.nextName();
if (readStr.equals("id")) {
id = reader.nextString();
} else if (readStr.equals("name")) {
name = reader.nextString();
} else if (readStr.equals("phonenumber")) {
phonenumber = reader.nextString();
} else if (readStr.equals("address")) {
address = reader.nextString();
}else {
reader.skipValue();
}
}
reader.endObject();
Log.d(TAG, id + "," + name + "," + phonenumber + "," + address);
return new MemberDTO(id, name, phonenumber, address);
}
}
▲LoginSelect.java
package com.example.my50_project.DTO;
import java.io.Serializable;
public class MyItem implements Serializable {
private String id, name, hire_date, image_path;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHire_date() {
return hire_date;
}
public void setHire_date(String hire_date) {
this.hire_date = hire_date;
}
public String getImage_path() {
return image_path;
}
public void setImage_path(String image_path) {
this.image_path = image_path;
}
public MyItem(String id, String name, String hire_date, String image_path) {
this.id = id;
this.name = name;
this.hire_date = hire_date;
this.image_path = image_path;
}
}
▲MyItem.java
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.my50_project"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
/*http 사용하는 라이브러리*/
useLibrary 'org.apache.http.legacy'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
/*안드로이드와 통신할 수 있는 버전 가져오기*/
implementation group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.5.1'
implementation('org.apache.httpcomponents:httpmime:4.3') {
exclude module: "httpclient"
}
implementation 'androidx.recyclerview:recyclerview:1.1.0'
/* 리사이클러 뷰 사용하기 */
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.android.support:design:29.0.0'
// URL 이미지로더
// 사용법
// Glide.with(this).load("http://www.selphone.co.kr/homepage/img/team/3.jpg").into(imageView);
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
▲build.gradle(:app)
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"> <!-- app:cardBackgroundColor="#FFFFFFFF" -->
<LinearLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center_vertical"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp">
<ImageView
android:id="@+id/iv_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center"
android:visibility="gone"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp"
android:weightSum="3">
<TextView
android:id="@+id/tv_id"
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="14sp"
android:textStyle="normal|bold"
android:gravity="center_vertical"
android:textAlignment="center" />
<TextView
android:id="@+id/tv_name"
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center_vertical"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="TextView"
android:textAlignment="center"
android:textSize="14sp"
android:textStyle="normal|bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
▲myitem_view.xml
package com.example.my50_project.Adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.my50_project.DTO.MyItem;
import com.example.my50_project.R;
import java.util.ArrayList;
import static com.example.my50_project.Sub1.selItem;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ItemViewHolder> {
private static final String TAG = "main MyRVAdapter";
Context mContext;
ArrayList<MyItem> arrayList;
public MyRecyclerViewAdapter(Context mContext, ArrayList<MyItem> arrayList) {
this.mContext = mContext;
this.arrayList = arrayList;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.myitem_view, parent);
return new ItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: " + position);
MyItem item = arrayList.get(position); //리스트의 인덱스를 가져와서
holder.setItem(item); //화면을 만든다
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: " + position); //position이 여기서 바뀌면 안되므로 final 속성을 붙이라는 경고가 뜬다
selItem = arrayList.get(position);
Toast.makeText(mContext, selItem.getName(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
//리사이클러뷰 내용 모두 지우기
public void removeAllItem() {
arrayList.clear();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
//객체 선언
LinearLayout parentLayout;
TextView id, name, date;
ImageView iv_image;
ProgressBar progressBar;
//뷰 홀더 클래스
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
//객체 초기화
parentLayout = itemView.findViewById(R.id.parentLayout);
id = itemView.findViewById(R.id.tv_id);
name = itemView.findViewById(R.id.tv_name);
date = itemView.findViewById(R.id.tv_date);
iv_image = itemView.findViewById(R.id.iv_img);
progressBar = itemView.findViewById(R.id.progressBar);
}
public void setItem(MyItem item) {
id.setText(item.getId());
name.setText(item.getName());
date.setText(item.getHire_date());
Glide.with(itemView).load(item.getImage_path()).into(iv_image);
}
}
}
▲MyRecyclerviewAdapter.java
○ 스프링 코드
package com.csslect.app.controller;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.AWTUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import com.csslect.app.command.ACommand;
import com.csslect.app.command.ADeleteMultiCommand;
import com.csslect.app.command.AInsertMultiCommand;
import com.csslect.app.command.AJoinCommand;
import com.csslect.app.command.ALoginCommand;
import com.csslect.app.command.ASelectMultiCommand;
import com.csslect.app.command.AUpdateMultiCommand;
import com.csslect.app.command.AUpdateMultiNoCommand;
@Controller
public class AController {
ACommand command;
@RequestMapping(value="/anLogin", method = {RequestMethod.GET, RequestMethod.POST} )
public String anLogin(HttpServletRequest req, Model model){
System.out.println("anLogin()");
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = (String) req.getParameter("id");
String passwd = (String) req.getParameter("passwd");
System.out.println(id);
System.out.println(passwd);
model.addAttribute("id", id);
model.addAttribute("passwd", passwd);
command = new ALoginCommand();
command.execute(model);
return "anLogin";
}
@RequestMapping(value="/anJoin", method = {RequestMethod.GET, RequestMethod.POST} )
public String anJoin(HttpServletRequest req, Model model){
System.out.println("anJoin()");
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String id = (String) req.getParameter("id");
String passwd = (String) req.getParameter("passwd");
String name = (String) req.getParameter("name");
String phonenumber = (String) req.getParameter("phonenumber");
String address = (String) req.getParameter("address");
System.out.println(id);
System.out.println(passwd);
System.out.println(name);
System.out.println(phonenumber);
System.out.println(address);
model.addAttribute("id", id);
model.addAttribute("passwd", passwd);
model.addAttribute("name", name);
model.addAttribute("phonenumber", phonenumber);
model.addAttribute("address", address);
command = new AJoinCommand();
command.execute(model);
return "anJoin";
}
@RequestMapping(value="/anSelectMulti", method = {RequestMethod.GET, RequestMethod.POST} )
public String anSelectMulti(HttpServletRequest req, Model model){
System.out.println("anSelectMulti()");
command = new ASelectMultiCommand();
command.execute(model);
return "anSelectMulti";
}
@RequestMapping(value="/anInsertMulti", method = {RequestMethod.GET, RequestMethod.POST} )
public String anInsertMulti(HttpServletRequest req, Model model){
System.out.println("anInsertMulti()");
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = (String) req.getParameter("id");
String name = (String) req.getParameter("name");
String date = (String) req.getParameter("date");
String dbImgPath = (String) req.getParameter("dbImgPath");
System.out.println(id);
System.out.println(name);
System.out.println(date);
System.out.println(dbImgPath);
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("date", date);
model.addAttribute("dbImgPath", dbImgPath);
MultipartRequest multi = (MultipartRequest)req;
MultipartFile file = multi.getFile("image");
if(file != null) {
String fileName = file.getOriginalFilename();
System.out.println(fileName);
// 디렉토리 존재하지 않으면 생성
makeDir(req);
if(file.getSize() > 0){
String realImgPath = req.getSession().getServletContext()
.getRealPath("/resources/");
System.out.println( fileName + " : " + realImgPath);
System.out.println( "fileSize : " + file.getSize());
try {
// 이미지파일 저장
file.transferTo(new File(realImgPath, fileName));
} catch (Exception e) {
e.printStackTrace();
}
}else{
// 이미지파일 실패시
fileName = "FileFail.jpg";
String realImgPath = req.getSession().getServletContext()
.getRealPath("/resources/" + fileName);
System.out.println(fileName + " : " + realImgPath);
}
}
command = new AInsertMultiCommand();
command.execute(model);
return "anInsertMulti";
}
public void makeDir(HttpServletRequest req){
File f = new File(req.getSession().getServletContext()
.getRealPath("/resources"));
if(!f.isDirectory()){
f.mkdir();
}
}
@RequestMapping(value="/anUpdateMulti", method = {RequestMethod.GET, RequestMethod.POST})
public void anUpdateMulti(HttpServletRequest req, Model model){
System.out.println("anUpdateMulti()");
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = (String) req.getParameter("id");
String name = (String) req.getParameter("name");
String date = (String) req.getParameter("date");
String dbImgPath = (String) req.getParameter("dbImgPath");
String pDbImgPath = (String) req.getParameter("pDbImgPath");
System.out.println(id);
System.out.println(name);
System.out.println(date);
System.out.println("Sub1Update:dbImgPath " + dbImgPath);
System.out.println("Sub1Update:pDbImgPath " + pDbImgPath);
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("date", date);
model.addAttribute("dbImgPath", dbImgPath);
// 이미지가 서로 같으면 삭제하지 않고 다르면 기존이미지 삭제
if(!dbImgPath.equals(pDbImgPath)){
String pFileName = req.getParameter("pDbImgPath").split("/")[req.getParameter("pDbImgPath").split("/").length -1];
String delDbImgPath = req.getSession().getServletContext().getRealPath("/resources/" + pFileName);
File delfile = new File(delDbImgPath);
System.out.println(delfile.getAbsolutePath());
if(delfile.exists()) {
boolean deleteFile = false;
while(deleteFile != true){
deleteFile = delfile.delete();
}
}//if(delfile.exists())
}//if(!dbImgPath.equals(pDbImgPath))
MultipartRequest multi = (MultipartRequest)req;
MultipartFile file = null;
file = multi.getFile("image");
if(file != null) {
String fileName = file.getOriginalFilename();
System.out.println(fileName);
// 디렉토리 존재하지 않으면 생성
makeDir(req);
if(file.getSize() > 0){
String realImgPath = req.getSession().getServletContext()
.getRealPath("/resources/");
System.out.println( fileName + " : " + realImgPath);
System.out.println( "fileSize : " + file.getSize());
try {
// 이미지파일 저장
file.transferTo(new File(realImgPath, fileName));
} catch (Exception e) {
e.printStackTrace();
}
}else{
fileName = "FileFail.jpg";
String realImgPath = req.getSession().getServletContext()
.getRealPath("/resources/" + fileName);
System.out.println(fileName + " : " + realImgPath);
}
}
command = new AUpdateMultiCommand();
command.execute(model);
}
@RequestMapping(value="/anUpdateMultiNo", method = {RequestMethod.GET, RequestMethod.POST})
public void anUpdateMultiNo(HttpServletRequest req, Model model){
System.out.println("anUpdateMultiNo()");
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = (String) req.getParameter("id");
String name = (String) req.getParameter("name");
String date = (String) req.getParameter("date");
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("date", date);
command = new AUpdateMultiNoCommand();
command.execute(model);
}
@RequestMapping(value="/anDeleteMulti", method = {RequestMethod.GET, RequestMethod.POST})
public void anDeleteMulti(HttpServletRequest req, Model model){
System.out.println("anDeleteMulti()");
model.addAttribute("id", req.getParameter("id"));
System.out.println((String)req.getParameter("id"));
System.out.println((String)req.getParameter("delDbImgPath"));
String pFileName = req.getParameter("delDbImgPath").split("/")[req.getParameter("delDbImgPath").split("/").length -1];
String delDbImgPath = req.getSession().getServletContext().getRealPath("/resources/" + pFileName);
// 이미지 파일지우기
File delfile = new File(delDbImgPath);
System.out.println(delfile.getAbsolutePath());
if(delfile.exists()) {
System.out.println("Sub1Del:pDelImagePath " + delfile.exists());
boolean deleteFile = false;
while(deleteFile != true){
deleteFile = delfile.delete();
}
}
command = new ADeleteMultiCommand();
command.execute(model);
}
}
▲AController.java
package com.csslect.app.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.csslect.app.dto.ANDto;
import com.csslect.app.dto.MemberDTO;
public class ANDao {
DataSource dataSource;
public ANDao() {
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:/comp/env/team01");
/*dataSource = (DataSource) context.lookup("java:/comp/env/CSS");*/
} catch (NamingException e) {
e.getMessage();
}
}
public MemberDTO anLogin(String idin, String passwdin) {
MemberDTO adto = null;
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select * "
+ " from member"
+ " where id = '" + idin
+ "' and passwd = '" + passwdin + "' ";
prepareStatement = connection.prepareStatement(query);
resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String phonenumber = resultSet.getString("phonenumber");
String address = resultSet.getString("address");
adto = new MemberDTO(id, name, phonenumber, address);
}
System.out.println("MemberDTO id : " + adto.getId());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return adto;
}
public int anJoin(String id, String passwd, String name,
String phonenumber, String address) {
Connection connection = null;
PreparedStatement prepareStatement = null;
int state = -100;
try {
connection = dataSource.getConnection();
String query = "insert into member(id, passwd, name, phonenumber, address) " +
"values('" + id + "', '" + passwd + "', '" + name + "', '" +
phonenumber + "', '" + address + "' )";
prepareStatement = connection.prepareStatement(query);
state = prepareStatement.executeUpdate();
if (state > 0) {
System.out.println(state + "삽입성공");
} else {
System.out.println(state + "삽입실패");
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return state;
}
public ArrayList<ANDto> anSelectMulti() {
ArrayList<ANDto> adtos = new ArrayList<ANDto>();
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select id, name, hire_date, image_path "
+ " from android"
+ " order by id desc";
prepareStatement = connection.prepareStatement(query);
resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
Date date = resultSet.getDate("hire_date");
String imagePath = resultSet.getString("image_path");
ANDto adto = new ANDto(id, name, date, imagePath);
adtos.add(adto);
}
System.out.println("adtos크기" + adtos.size());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return adtos;
}
public int anInsertMulti(int id, String name, String date, String dbImgPath) {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
int state = -1;
try {
//
connection = dataSource.getConnection();
String query = "insert into android(id, name, hire_date, image_path) " + "values(" + id + ",'"
+ name + "'," + "to_date('" + date + "','rr/mm/dd') , '" + dbImgPath + "' )";
prepareStatement = connection.prepareStatement(query);
state = prepareStatement.executeUpdate();
if (state > 0) {
System.out.println(state + "삽입성공");
} else {
System.out.println(state + "삽입실패");
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return state;
}
public int anUpdateMulti(int id, String name, String date, String dbImgPath) {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
int state = -1;
try {
// 아이디는 수정할수 없음
connection = dataSource.getConnection();
String query = "update android set "
+ " name = '" + name + "' "
+ ", hire_date = '" + date + "' "
+ ", image_path = '" + dbImgPath + "' "
+ " where id = " + id ;
prepareStatement = connection.prepareStatement(query);
state = prepareStatement.executeUpdate();
if (state > 0) {
System.out.println("수정1성공");
} else {
System.out.println("수정1실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return state;
}
public int anUpdateMultiNo(int id, String name, String date) {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
int state = -1;
try {
// 아이디는 수정할수 없음
connection = dataSource.getConnection();
String query = "update android set "
+ " name = '" + name + "' "
+ ", hire_date = '" + date + "' "
+ " where id = " + id ;
prepareStatement = connection.prepareStatement(query);
state = prepareStatement.executeUpdate();
if (state > 0) {
System.out.println("수정2성공");
} else {
System.out.println("수정2실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return state;
}
public int anDeleteMulti(int id) {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
int state = -1;
try {
connection = dataSource.getConnection();
String query = "delete from android where id=" + id;
System.out.println(id);
prepareStatement = connection.prepareStatement(query);
state = prepareStatement.executeUpdate();
if (state > 0) {
System.out.println("삭제성공");
} else {
System.out.println("삭제실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return state;
}
}
▲ANDao.java
package com.csslect.app.dto;
import java.sql.Date;
public class ANDto {
int id;
String name;
Date hire_date;
String image_path;
public ANDto() {
}
public ANDto(int id, String name, Date hire_date, String image_path) {
this.id = id;
this.name = name;
this.hire_date = hire_date;
this.image_path = image_path;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHire_date(Date hire_date) {
this.hire_date = hire_date;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Date getHire_date() {
return hire_date;
}
public String getImage_path() {
return image_path;
}
public void setImage_path(String image_path) {
this.image_path = image_path;
}
}
▲ANDto.java
package com.csslect.app.command;
import java.util.ArrayList;
import org.springframework.ui.Model;
import com.csslect.app.dao.ANDao;
import com.csslect.app.dto.ANDto;
public class ASelectMultiCommand implements ACommand{
@Override
public void execute(Model model) {
ANDao adao = new ANDao();
ArrayList<ANDto> adtos = adao.anSelectMulti();
model.addAttribute("anSelectMulti", adtos);
}
}
▲ASelectMultiCommand.java
<%@page import="com.csslect.app.dto.ANDto"%>
<%@page import="com.google.gson.Gson"%>
<%@page import="com.google.gson.JsonObject"%>
<%@page import="org.springframework.ui.Model"%>
<%@page import="java.sql.*, java.sql.Date, javax.sql.*, javax.naming.*,
java.util.*, java.io.PrintWriter" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
Gson gson = new Gson();
String json = gson.toJson((ArrayList<ANDto>)request.getAttribute("anSelectMulti"));
out.println(json);
/* try{
out.println("<lists>");
for(ANDto dto : (ArrayList<ANDto>)request.getAttribute("anSelectMulti")){
out.println("<list>");
out.println("<id>" + dto.getId() + "</id>");
out.println("<name>" + dto.getName() + "</name>");
out.println("<date>" + dto.getHire_date() + "</date>");
out.println("<image>" + dto.getImage1() + "</image>");
out.println("<uploadtype>" + dto.getUploadtype() + "</uploadtype>");
if(dto.getUploadtype().equals("video")){
String fileNamePath = (dto.getImage1().split("/")[dto.getImage1().split("/").length-1]);
String replacePath = (dto.getImage1().split("/")[dto.getImage1().split("/").length-1]).replace(".", "_");
System.out.println("replacePath :" + replacePath);
String videoImagePath = dto.getImage1().replace(fileNamePath, "videoImages/" + replacePath) + ".jpg";
System.out.println("videoImagePath :" + videoImagePath);
out.println("<videoimage>" + videoImagePath + "</videoimage>");
}else if(dto.getUploadtype().equals("image")){
out.println("<videoimage>" + "novideo" + "</videoimage>");
}
out.println("</list>");
}
out.println("</lists>");
}catch(Exception e) {
System.out.println("select list failed" + e.getMessage());
} */
%>
▲anSelectMulti.jsp
'취업성공패키지 SW 개발자 교육 > Android' 카테고리의 다른 글
[Android] 31. 안드로이드 & DB & 서버(스프링) 연동 ③ : 로그인 후 화면 출력, 사진 클릭, 사진 촬영 후 등록, 앨범에 있는 사진 등록 (0) | 2020.06.19 |
---|---|
[Android] 29. 안드로이드 & DB & 서버(스프링) 연동 ① : 회원가입, 로그인 구현 (0) | 2020.06.17 |
[Do it! 안드로이드] 03. 첫 번째 앱 만들기 / 03-1. 첫 프로젝트 만들기 (0) | 2020.06.16 |
[Android] 28. AsyncTask (0) | 2020.06.16 |
[Do it! 안드로이드] I. Hello! 안드로이드 / 01. 안드로이드란? / 01-1. 안드로이드 이해하기 / 01-2. 안드로이드의 흐름 살펴보기 (0) | 2020.06.15 |