๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋…ธ๋ ฅ์„ ์ด๊ธฐ๋Š” ์žฌ๋Šฅ์€ ์—†๊ณ 
๋…ธ๋ ฅ์„ ์™ธ๋ฉดํ•˜๋Š” ๊ฒฐ๊ณผ๋„ ์—†๋‹ค.
- ์ด์ฐฝํ˜ธ 9๋‹จ

D E V E L O P M E N T/Android

[Andriod] ์•ˆ๋“œ๋กœ์ด๋“œ Retrofit, ์›น์„œ๋ฒ„๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ๋งŒ๋“ค๊ธฐ

  Junesker   2021. 6. 21.
๋ฐ˜์‘ํ˜•

ํšŒ์‚ฌ์—์„œ ๊ธฐ์กด์— ๋งŒ๋“ค์–ด์ง„ ์–ดํ”Œ์— ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์ด ์—†์–ด์„œ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์€ ์ž‘๊ฒŒ๋‚˜๋งˆ ๋งŒ๋“ค์–ด๋ด์„œ ๊นŠ์€ ์ง€์‹์€ ์—†์ง€๋งŒ, ์ด๋ฒˆ์— ๋˜ ํ•œ๊ฐ€์ง€ ๋ฐฐ์›Œ๊ฐ€๋Š”๊ฒƒ ๊ฐ™์•„ ์˜๋ฏธ๊ฐ€ ์žˆ์—ˆ๋˜ ์ž‘์—…์ด๋‹ค. ์ฒ˜์Œ์—” Retrofit์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ์†๋„๋‚˜ ํ†ต์‹ ๋ฐฉ๋ฒ• ๋“ฑ Retrofit์ด ์„ฑ๋Šฅ์ ์œผ๋กœ ๋” ์ข‹๋‹ค๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ๋‹ค.

 

 

 

 

Retrofit, ์›น ์„œ๋ฒ„๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ๋งŒ๋“ค๊ธฐ ์ˆœ์„œ
1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
2. Activityํ™”๋ฉด๋“ค ์ƒ์„ฑ(๋กœ๊ทธ์ธ, ๊ฒฐ๊ณผํŽ˜์ด์ง€)
3. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ํ™˜๊ฒฝ์„ค์ •
4. ํŽ˜์ด์ง€๋ณ„ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ ์„œ๋น„์Šค, VO ๋“ฑ ์ƒ์„ฑ
5. ์›น ์„œ๋ฒ„ ๊ตฌ์ถ•(egovFramework ๊ธฐ๋ฐ˜์˜ API ์›น ์„œ๋ฒ„ ๊ตฌ์ถ•)
6. ํ…Œ์ŠคํŠธ

 

 

 

 

 

๋กœ๊ทธ์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

 

 

 

1.  ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.

 

New Project ๋งŒ๋“ค๊ธฐ

File > New > New Project ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์ง„ํ–‰ํ•œ๋‹ค. Activityํ™”๋ฉด์„ ์„ ํƒํ•˜๋Š” ๋ž€์—์„œ๋Š” ๋กœ๊ทธ์ธ Activity๋ฅผ ์„ ํƒํ–ˆ๋‹ค๊ฐ€ ์ƒˆ๋กญ๊ฒŒ ํ™”๋ฉด๋“ค์„ ๊ตฌ์„ฑํ•˜๊ณ ์ž Empty Activity๋ฅผ ์„ ํƒํ–ˆ๊ณ , RetrofitLoginTest ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค.

Name : RetrofitLoginTest
Package name : com.example.retrofitlogintest
Language : Java
Minimum SDK : API 16: Android 4.1
Use legacy android.support libraries - check!

 

 

 

 

 

 

๋กœ๊ทธ์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ Activityํ™”๋ฉด๋“ค ์ƒ์„ฑ(๋กœ๊ทธ์ธ, ๊ฒฐ๊ณผํŽ˜์ด์ง€)

 

 

 

 

โ–’ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์ƒ์„ฑ

 

 

 

 

2.  res/layout/activity_main.xml ํŒŒ์ผ์„ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ˆ˜์ •ํ•œ๋‹ค.

 

activity_main.xml ํŒŒ์ผ ๋‚ด, Designํƒญ์—์„œ ์›ํ•˜๋Š” ๋กœ๊ทธ์ธ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•œ๋‹ค. ์ž‘์„ฑ์ž๋Š” ID,PW์ž…๋ ฅ๋ž€๊ณผ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋””์ž์ธํ–ˆ๋‹ค. 

๊ฐ ์ปดํฌ๋„ŒํŠธ๋“ค์˜ id์™€ Constraint Widget, Text๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

Component Widget EditText 'ID'
id : editText_id
Constraint Widget : ์™ผ(100), ์œ„(340), ์˜ค(100), ์•„๋ž˜(10)
Text : ๊ณต๋ฐฑ

Component Widget EditText 'PW'
id : editText_pw
Constraint Widget : ์™ผ(100), ์œ„(10), ์˜ค(100), ์•„๋ž˜(10)
Text : ๊ณต๋ฐฑ

Component Widget Button 'Login'
id : btn_login
Constraint Widget : ์™ผ(160), ์œ„(10), ์˜ค(160), ์•„๋ž˜(215)
Text : LOGIN

 

[Activity_main.xml] Source

<?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=".MainActivity">

    <EditText
        android:id="@+id/editText_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="340dp"
        android:layout_marginEnd="100dp"
        android:layout_marginRight="100dp"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/editText_pw"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText_pw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="100dp"
        android:layout_marginRight="100dp"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintBottom_toTopOf="@+id/btn_login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_id" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="160dp"
        android:layout_marginRight="160dp"
        android:layout_marginBottom="215dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_pw" />
</android.support.constraint.ConstraintLayout>

 

 

 

 

โ–’ ๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ ํŽ˜์ด์ง€์ƒ์„ฑ

 

 

 

 

3.  activity_result.xml ํŒŒ์ผ์„ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

activity_result.xml ํŒŒ์ผ ์ƒ์„ฑ

activity_result.xml ํŒŒ์ผ ๋‚ด, Designํƒญ์—์„œ ์›ํ•˜๋Š” ๋กœ๊ทธ์ธ ๊ฒฐ๊ณผ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•œ๋‹ค. ์ž‘์„ฑ์ž๋Š” ๋กœ๊ทธ์ธ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก TextView์„ ๋””์ž์ธํ–ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ์˜ id์™€ Constraint Widget, Text๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

Component Widget TextView 'Result'
id : textView_result
Constraint Widget : ์™ผ(20), ์œ„(320), ์˜ค(20), ์•„๋ž˜(320)
Text : ๊ณต๋ฐฑ

 

 

[Activity_result.xml] Source

<?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">

    <TextView
        android:id="@+id/textView_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="320dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="320dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

 

 

 

 

 

๋กœ๊ทธ์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ํ™˜๊ฒฝ์„ค์ •

 

 

 

โ–’ build.gradle

 

 

 

4.  build.gradle, AndroidManifest.xml์—์„œ ์ถ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ์„ค์ •๊ฐ’์„ ์…‹ํŒ…ํ•œ๋‹ค.

 

[build.gradle] Source

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.retrofitlogintest"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
}

Retrofit์„ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ์ปดํŒŒ์ผ ์„ค์ • ๋“ฑ๋“ฑ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ build.gradleํŒŒ์ผ์„ ์ถ”๊ฐ€ ๋ฐ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

 

 

โ–’ AndroidManifest.xml

 

 

 

[AndroidManifest.xml] Source

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋„คํŠธ์›Œํฌ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋งค๋‹ˆํŽ˜์ŠคํŠธ์— ๋‹ค์Œ ๊ถŒํ•œ์„ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค. 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.retrofitlogintest">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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/Theme.RetrofitLoginTest"
        android:usesCleartextTraffic="true">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ResultActivity"/>
    </application>

</manifest>

android.permission.INTERNET, android.permission.ACCESS_NETWORK_STATE๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋ฐœ์ƒํ–ˆ๋˜ ๋ฌธ์ œ์  ์ค‘์—์„œ, http://์™€ https://์ค‘์—์„œ ๋ ‰ ์„œ๋ฒ„ ๋‚ด, ์…‹ํŒ…๋œ ์›น ์„œ๋ฒ„๋Š” http://๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ ์ด ์š”์ฒญ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” android:usesCleartextTraffic="true"์„ค์ •๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค๋Š”๊ฑธ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด์„œ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฐ๊ฒฐ๋œ ํŽ˜์ด์ง€์˜ Activity๋“ค๋„ activity ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

 

 

 

5.  build.gradle์—์„œ ์ถ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ์„ค์ •๊ฐ’์„ ์…‹ํŒ…ํ•œ๋‹ค.

 

ํ”„๋กœ์ ํŠธ ์ „์ฒด ๋‚ด์šฉ์ด๋‹ค. ํŽ˜์ด์ง€์™€ ํ™˜๊ฒฝ์„ค์ • ๋ถ€๋ถ„์ด ๋๋‚ฌ์œผ๋‹ˆ ๊ด€๋ จ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ ์„œ๋น„์Šค,VO๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ์ „์ฒด ๊ตฌ์กฐ๋Š” ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™๋‹ค.

 

 

 

 

 

 

ํŽ˜์ด์ง€๋ณ„ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ ์„œ๋น„์Šค, VO ๋“ฑ ์ƒ์„ฑ

 

 

 

 

 

 

 

 

6.  MemberVO, ILoginService, MainActivity, ResultActivity ๋“ฑ ์ƒ์„ฑํ•œ๋‹ค.

 

๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

[MemberVO] Source

package com.example.retrofitlogintest.vo;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

public class MemberVO implements Parcelable {
    @SerializedName("mem_no")
    private int mem_no;
    @SerializedName("mem_id")
    private String mem_id;
    @SerializedName("mem_pw")
    private String mem_pw;
    @SerializedName("mem_name")
    private String mem_name;

    public MemberVO(String mem_id, String mem_pw){
        this.mem_id = mem_id;
        this.mem_pw = mem_pw;
    }

    public int getMem_no(){
        return mem_no;
    }
    public void setMem_no(int mem_no){
        this.mem_no = mem_no;
    }
    public String getMem_id(){
        return mem_id;
    }
    public void setMem_id(String mem_id){
        this.mem_id = mem_id;
    }
    public String getMem_pw(){
        return mem_pw;
    }
    public void setMem_pw(String mem_pw){
        this.mem_pw = mem_pw;
    }
    public String getMem_name(){
        return mem_name;
    }
    public void setMem_name(String mem_name){
        this.mem_name = mem_name;
    }

    protected MemberVO(Parcel in) {
        mem_no = in.readInt();
        mem_id = in.readString();
        mem_pw = in.readString();
        mem_name = in.readString();
    }

    public static final Creator<MemberVO> CREATOR = new Creator<MemberVO>() {
        @Override
        public MemberVO createFromParcel(Parcel in) {
            return new MemberVO(in);
        }

        @Override
        public MemberVO[] newArray(int size) {
            return new MemberVO[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(mem_no);
        parcel.writeString(mem_id);
        parcel.writeString(mem_pw);
        parcel.writeString(mem_name);
    }
}

implements Parcelable์„ ์„ค์ •ํ•œ ์ด์œ ๋Š” ๊ฐ๊ฐ์˜ Activity๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค„๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‹ค. MemberVO๋ฅผ ์ƒ์„ฑํ• ๋•Œ ๊ธฐ๋ณธ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ด์•„ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

 

 

 

[ILgoinService] Source

package com.example.retrofitlogintest.login;

import com.example.retrofitlogintest.vo.MemberVO;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ILoginService {

    @GET("app/getMember.do")
    Call<MemberVO> getMember(@Query("mem_id") String mem_id, @Query("mem_pw") String mem_pw);
}

์ž…๋ ฅํ•œ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ†ตํ•ด DB์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ํšŒ์›์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ฟผ๋ฆฌ ๋‚ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธฐ๊ธฐ์œ„ํ•ด @Query ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…์„ ์…‹ํŒ…ํ–ˆ๋‹ค.

 

 

 

 

 

[MainActivity] Source

package com.example.retrofitlogintest;

import android.content.Intent;
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.example.retrofitlogintest.login.ILoginService;
import com.example.retrofitlogintest.vo.MemberVO;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private EditText editTextID ,editTextPW;    // ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ์ฐฝ
    private Button btnLogin;                    // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ
    private Retrofit retrofit;                    // ์›น์„œ๋ฒ„์™€ ํ†ต์‹ ํ•  Retrofit

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Retrofit Login");

        setRetrofitInit();  // Retrofit ์ดˆ๊ธฐํ™”

        editTextID = findViewById(R.id.editText_id);
        editTextPW = findViewById(R.id.editText_pw);
        btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();    // ๋ฒ„ํŠผ ํด๋ฆญ์‹œ, ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
            }
        });
    }

    private void setRetrofitInit(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.105:8081")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    private void login(){
        String id = editTextID.getText().toString();
        String pw = editTextPW.getText().toString();

        ILoginService service = retrofit.create(ILoginService.class);
        Call<MemberVO> call = service.getMember(id, pw);

        call.enqueue(new Callback<MemberVO>() {
            @Override
            public void onResponse(Call<MemberVO> call, Response<MemberVO> response) {
                MemberVO memberVO = response.body();    // ์›น์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต๋ฐ›์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค.

                if(memberVO != null){       // ํšŒ์›์ž…๋‹ˆ๋‹ค.
                    Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
                    intent.putExtra("memberVO", memberVO);
                    startActivity(intent);
                }else{                      // ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค.
                    Toast.makeText(getApplicationContext(), "ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<MemberVO> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "๋กœ๊ทธ์ธ์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

 

 

 

 

[ResultActivity] Source

package com.example.retrofitlogintest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.retrofitlogintest.vo.MemberVO;

public class ResultActivity extends AppCompatActivity {

    private TextView textView_result;       // ๊ฒฐ๊ณผ

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        setTitle("Result");

        textView_result = findViewById(R.id.textView_result);

        Intent intent = getIntent();        // ๋กœ๊ทธ์ธ ์‹œ, ๋„˜๊ฒจ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.
        MemberVO memberVO = intent.getParcelableExtra("memberVO");

        textView_result.setText(memberVO.getMem_name() + "๋‹˜! ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค!");
    }

}

 

 

 

 

 

[์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ๊ฒฐ๊ณผ]

 

 

 

 

 

์›น ์„œ๋ฒ„ ๊ตฌ์ถ•(egovFramework ๊ธฐ๋ฐ˜์˜ API ์›น ์„œ๋ฒ„ ๊ตฌ์ถ•)

 

 

 

 

 

7.  egovframework ๊ธฐ๋ฐ˜์˜ ์›น์„œ๋ฒ„ ๊ตฌ์ถ•์€ ๊ฒŒ์‹œ๊ธ€ ๋งํฌ๋กœ ๋Œ€์‹ ํ•œ๋‹ค.

 

[์›น ์„œ๋ฒ„ ๋งํฌ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ]์›น ์„œ๋ฒ„ ๋งํฌ๋Š” ๊ณง ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ํ›„ ๋น ๋ฅธ ์‹œ์ผ๋‚ด๋กœ ์—…๋กœ๋“œ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

 

 

 

 

 

 

[Andriod] ์•ˆ๋“œ๋กœ์ด๋“œ Retrofit, ์›น์„œ๋ฒ„๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ๋งŒ๋“ค๊ธฐ


@Junesker

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€