From 5d2370599ce40d5951a756bac1f0eb3ae2e0aa05 Mon Sep 17 00:00:00 2001 From: hyeumm <20221159@sungshin.ac.kr> Date: Wed, 1 May 2024 23:58:13 +0900 Subject: [PATCH 01/36] =?UTF-8?q?feat#10/:=204=EC=B0=A8=20=EC=84=B8?= =?UTF-8?q?=EB=AF=B8=EB=82=98=20api=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 16 +++++ app/src/main/AndroidManifest.xml | 3 +- app/src/main/java/com/sopt/now/ApiFactory.kt | 23 ++++++ app/src/main/java/com/sopt/now/AuthService.kt | 12 ++++ .../java/com/sopt/now/RequestSignUpDto.kt | 16 +++++ .../java/com/sopt/now/ResponseSignUpDto.kt | 12 ++++ .../main/java/com/sopt/now/SignUpActivity.kt | 70 ++++++++++++++++++- app/src/main/res/layout/activity_sign_up.xml | 56 +++++++-------- app/src/main/res/values/strings.xml | 4 +- 9 files changed, 180 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/com/sopt/now/ApiFactory.kt create mode 100644 app/src/main/java/com/sopt/now/AuthService.kt create mode 100644 app/src/main/java/com/sopt/now/RequestSignUpDto.kt create mode 100644 app/src/main/java/com/sopt/now/ResponseSignUpDto.kt diff --git a/app/build.gradle b/app/build.gradle index 2a5b0d9..528493f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,8 +1,12 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0' } +Properties properties = new Properties() +properties.load(project.rootProject.file('local.properties').newDataInputStream()) + android { namespace 'com.sopt.now' compileSdk 34 @@ -15,6 +19,7 @@ android { versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + buildConfigField "String", "AUTH_BASE_URL", properties["base.url"] } buildTypes { @@ -32,6 +37,7 @@ android { } buildFeatures { viewBinding = true + buildConfig true } } @@ -50,4 +56,14 @@ dependencies { implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2" implementation "androidx.fragment:fragment-ktx:1.6.1" implementation "androidx.activity:activity-ktx:1.8.0" + + //api + implementation 'com.squareup.retrofit2:retrofit:2.9.0' + implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1' + implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0' + // define a BOM and its version + implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0")) + // define any required OkHttp artifacts without version + implementation("com.squareup.okhttp3:okhttp") + implementation("com.squareup.okhttp3:logging-interceptor") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1466163..8f9e8ed 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ - + create(): T = retrofit.create(T::class.java) +} + +object ServicePool { + val authService = ApiFactory.create() +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/AuthService.kt b/app/src/main/java/com/sopt/now/AuthService.kt new file mode 100644 index 0000000..c08c089 --- /dev/null +++ b/app/src/main/java/com/sopt/now/AuthService.kt @@ -0,0 +1,12 @@ +package com.sopt.now + +import retrofit2.Call +import retrofit2.http.Body +import retrofit2.http.POST + +interface AuthService { + @POST("member/join") //http 메소드 + fun signUp( + @Body request: RequestSignUpDto, + ): Call //비동기->callback +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/RequestSignUpDto.kt b/app/src/main/java/com/sopt/now/RequestSignUpDto.kt new file mode 100644 index 0000000..2763e87 --- /dev/null +++ b/app/src/main/java/com/sopt/now/RequestSignUpDto.kt @@ -0,0 +1,16 @@ +package com.sopt.now + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class RequestSignUpDto( + @SerialName("authenticationId") + val authenticationId: String, + @SerialName("password") + val password: String, + @SerialName("nickname") + val nickname: String, + @SerialName("phone") + val phone: String, +) diff --git a/app/src/main/java/com/sopt/now/ResponseSignUpDto.kt b/app/src/main/java/com/sopt/now/ResponseSignUpDto.kt new file mode 100644 index 0000000..ce55efe --- /dev/null +++ b/app/src/main/java/com/sopt/now/ResponseSignUpDto.kt @@ -0,0 +1,12 @@ +package com.sopt.now + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ResponseSignUpDto( + @SerialName("code") + val code: Int, + @SerialName("message") + val message: String, +) diff --git a/app/src/main/java/com/sopt/now/SignUpActivity.kt b/app/src/main/java/com/sopt/now/SignUpActivity.kt index 48b0d23..3178424 100644 --- a/app/src/main/java/com/sopt/now/SignUpActivity.kt +++ b/app/src/main/java/com/sopt/now/SignUpActivity.kt @@ -1,11 +1,76 @@ package com.sopt.now -import android.content.Intent import android.os.Bundle +import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.sopt.now.databinding.ActivitySignUpBinding +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +class SignUpActivity : AppCompatActivity() { + + private val binding by lazy { ActivitySignUpBinding.inflate(layoutInflater) } + private val authService by lazy { ServicePool.authService } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(binding.root) + initViews() + } + + private fun initViews() { + binding.btnSignUp.setOnClickListener { + signUp() + } + } + + private fun signUp() { + val signUpRequest = getSignUpRequestDto() + authService.signUp(signUpRequest).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + if (response.isSuccessful) { + val data: ResponseSignUpDto? = response.body() + val userId = response.headers()["location"] + Toast.makeText( + this@SignUpActivity, + "회원가입 성공 userID는 $userId 입니다", + Toast.LENGTH_SHORT, + ).show() + Log.d("SignUp", "data: $data, userId: $userId") + } else { + val error = response.message() + Toast.makeText( + this@SignUpActivity, + "회원가입에 실패했습니다.$error", + Toast.LENGTH_SHORT, + ).show() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText(this@SignUpActivity, "서버 에러 발생 ", Toast.LENGTH_SHORT).show() + } + }) + } + + private fun getSignUpRequestDto(): RequestSignUpDto { + val id = binding.etvSignUpId.text.toString() + val password = binding.etvSignUpPw.text.toString() + val nickname = binding.etvSignUpNick.text.toString() + val phoneNumber = binding.etvSignUpPhone.text.toString() + return RequestSignUpDto( + authenticationId = id, + password = password, + nickname = nickname, + phone = phoneNumber + ) + } +} +/** class SignUpActivity : AppCompatActivity() { private lateinit var binding: ActivitySignUpBinding @@ -60,4 +125,5 @@ class SignUpActivity : AppCompatActivity() { const val MIN_PW_LENGTH = 8 const val MAX_PW_LENGTH = 12 } -} \ No newline at end of file +} + */ \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sign_up.xml b/app/src/main/res/layout/activity_sign_up.xml index 91ceac3..acce929 100644 --- a/app/src/main/res/layout/activity_sign_up.xml +++ b/app/src/main/res/layout/activity_sign_up.xml @@ -8,7 +8,7 @@ tools:context=".SignUpActivity">