-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seminar4 #5
base: main
Are you sure you want to change the base?
Seminar4 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.data | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class RequsetLoginData( | ||
@SerializedName("email") | ||
val email : String, | ||
val password : String, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.data | ||
|
||
data class RequsetSignUpdata( | ||
val email : String, | ||
val name : String, | ||
val password : String, | ||
|
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.data | ||
|
||
data class ResponseLoginData( | ||
val status : Int, | ||
val success : Boolean, | ||
val message : String, | ||
val data : Data | ||
) { | ||
|
||
data class Data( | ||
val id: Int, | ||
val name: String, | ||
val email: String | ||
) | ||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data라는 이름이 좀더 명확했으면 좋겠어. 이건 사람의 정보인 것 같은데 Data는 이걸 포함하고도 한참 넓은 범주인 것 같아 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.data | ||
|
||
data class ResponseSignUpData( | ||
|
||
val status : Int, | ||
val success : Boolean, | ||
val message : String, | ||
val data : Data | ||
) { | ||
|
||
data class Data( | ||
val id: Int, | ||
val name: String, | ||
val email: String | ||
|
||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.di | ||
|
||
import android.telecom.Call | ||
import com.example.a29th_first_semina.ui.view.sigin.data.RequsetLoginData | ||
import com.example.a29th_first_semina.ui.view.sigin.data.RequsetSignUpdata | ||
import com.example.a29th_first_semina.ui.view.sigin.data.ResponseLoginData | ||
import com.example.a29th_first_semina.ui.view.sigin.data.ResponseSignUpData | ||
import retrofit2.http.Body | ||
import retrofit2.http.Headers | ||
import retrofit2.http.POST | ||
|
||
interface Service { | ||
@Headers("Content-Type:application/json") | ||
@POST("user/login") | ||
fun postLogin( | ||
@Body body : RequsetLoginData | ||
) : retrofit2.Call <ResponseLoginData> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필요없는 prefix는 지우셔도 되어요 |
||
|
||
|
||
@POST("user/signup") | ||
fun postSignup( | ||
@Body body : RequsetSignUpdata | ||
) : retrofit2.Call <ResponseSignUpData> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.di | ||
|
||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
object ServiceCreater { | ||
|
||
private const val BASE_URL = "https://asia-northeast3-we-sopt-29.cloudfunctions.net/api/" | ||
|
||
private val retrofit : Retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
val service : Service = retrofit.create(Service::class.java) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안드로이드 자체에 실제로 Service라는 컴포넌트가 있어서 헷갈리지 않게 작명 유의하면 더 좋을 것 같아요 |
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.example.a29th_first_semina.ui.view.sigin.view | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Toast | ||
import com.example.a29th_first_semina.databinding.ActivitySigninBinding | ||
import com.example.a29th_first_semina.ui.view.home.HomeActivity | ||
import com.example.a29th_first_semina.ui.view.sigin.di.ServiceCreater | ||
import com.example.a29th_first_semina.ui.view.sigin.data.RequsetLoginData | ||
import com.example.a29th_first_semina.ui.view.sigin.data.ResponseLoginData | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class SignInActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivitySigninBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivitySigninBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
initButtonClickEvent() | ||
|
||
} | ||
|
||
private fun initButtonClickEvent() { | ||
|
||
clickLoginEvent() | ||
clickSignUpEvent() | ||
} | ||
|
||
private fun moveHomeActivity() { | ||
val homeIntent = Intent(this, HomeActivity::class.java) | ||
startActivity(homeIntent) | ||
} | ||
|
||
private fun moveSignUpActivity() { | ||
val signupIntent = Intent(this, SignUpActivity::class.java) | ||
startActivity(signupIntent) | ||
|
||
} | ||
|
||
private fun clickLoginEvent() { | ||
|
||
with(binding) { | ||
btnLogin.setOnClickListener { | ||
val userId = etId.text | ||
val userPassword= etPwd.text | ||
if (userId.isEmpty() || userPassword.isEmpty()) { | ||
Toast.makeText(this@SignInActivity, "아이디/비번 둘 다 입력해라", Toast.LENGTH_SHORT).show() | ||
} else { | ||
initNetwork() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
private fun clickSignUpEvent() { | ||
binding.tvSignup.setOnClickListener { | ||
moveSignUpActivity() | ||
} | ||
|
||
} | ||
|
||
private fun initNetwork(){ | ||
val requsetLoginData = RequsetLoginData( | ||
email = binding.etId.text.toString(), | ||
password = binding.etPwd.text.toString() | ||
) | ||
|
||
val call : retrofit2.Call<ResponseLoginData> = ServiceCreater.service.postLogin(requsetLoginData) | ||
|
||
call.enqueue(object : Callback<ResponseLoginData>{ | ||
override fun onResponse( | ||
call: retrofit2.Call<ResponseLoginData>, | ||
response: Response<ResponseLoginData> | ||
) { | ||
if(response.isSuccessful){ | ||
val data = response.body()?.data | ||
moveHomeActivity() | ||
Log.d("서버통신 상태",response.body()?.status.toString()) | ||
} | ||
else { | ||
Toast.makeText( | ||
this@SignInActivity, | ||
response.body()?.status.toString(), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
Log.d("서버통신 상태", response.body()?.status.toString()) | ||
} | ||
} | ||
|
||
override fun onFailure(call: retrofit2.Call<ResponseLoginData>, t: Throwable) { | ||
Log.d("로그인통신 실패","실패임") | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gson에서는 변수명과 받아오는 데이터의 키가 같으면 SerializedName 안붙이셔도 됩니당~