팀 프로젝트 하면서 사용했던 여러 api 연결 방법들
* 공통적으로 Header를 사용했으나 다른 곳에서는 사용하지 않을 수 있음
1. Parameter (파라미터) 없는 경우
DTO
data class SigninDTO (
val status: Int,
val isSuccess: Boolean,
val accessToken: String?,
val has: Boolean?,
val message: String?
)
Interface
interface SigninInterface {
@POST("링크")
fun getSignin(
@Header("Authorization") accessToken: String
): Call<SigninDTO>
}
Service
class SigninService {
val retrofit = Retrofit.Builder()
.baseUrl("기본 링크")
.addConverterFactory(GsonConverterFactory.create())
.build()
val signinService = retrofit.create(SigninInterface::class.java)
fun executeSignIn(accessToken: String, callback: (SigninDTO?) -> Unit) {
val call = signinService.getSignin("$accessToken")
call.enqueue(object : Callback<SigninDTO> {
override fun onResponse(call: Call<SigninDTO>, response: Response<SigninDTO>) {
when (response.code()) {
200 -> {
callback(response.body())
}
500 -> {
callback(response.body())
}
4001 -> {
callback(response.body())
}
else -> {
callback(null)
}
}
}
override fun onFailure(call: Call<SigninDTO>, t: Throwable) {
callback(null)
}
})
}
}
Activity/Fragment
val signinService = SigninService()
signinService.executeSignIn(token.accessToken) { signinDTO ->
if (signinDTO != null) {
if (signinDTO.isSuccess.toString() == "true") {
if (signinDTO.has == true){
}
else {
}
}
} else {
}
}
2. Path 있는 경우
DTO
data class TodayMailResponse(
val status: Long,
val isSuccess: Boolean,
val post: List<Post>,
)
data class Post(
@SerializedName("postbox_idx") val postboxIdx: Long,
@SerializedName("sender_idx") val senderIdx: String,
val voice: Long,
val content: String?,
@SerializedName("is_read") val isRead: Long,
)
Interface
interface MailInterface {
@GET("링크/{today}")
fun mailToday(
@Header("Authorization") accessToken: String,
@Path("today") today: String
): Call<TodayMailResponse>
}
Service
class MailService {
val retrofit = Retrofit.Builder()
.baseUrl("기본 링크")
.addConverterFactory(GsonConverterFactory.create())
.build()
private val mailService = retrofit.create(MailInterface::class.java)
fun todayMail(accessToken: String, today: String, callback: (TodayMailResponse?) -> Unit) {
val call = mailService.mailToday("$accessToken", today)
call.enqueue(object : Callback<TodayMailResponse> {
override fun onResponse(call: Call<TodayMailResponse>, response: Response<TodayMailResponse>) {
when (response.code()) {
200 -> {
callback(response.body())
}
401 -> {
callback(response.body())
}
500 -> {
callback(response.body())
}
4001 -> {
callback(response.body())
}
else -> {
callback(null)
}
}
}
override fun onFailure(call: Call<TodayMailResponse>, t: Throwable) {
callback(null)
}
})
}
}
Activity/Fragment
val mailService = MailService()
mailService.todayMail(loadedToken, today.toString()) { mailDTO ->
if (mailDTO != null) {
if (mailDTO.isSuccess.toString() == "true") {
}
} else {
}
}
3. formData 있는 경우
(안드로이드 팀장님의 도움을 많이 받아 해결하였다)
DTO
data class VoiceResponse (
val status: Int,
val isSuccess: Boolean,
val message: String
)
Interface
interface VoiceInterface {
@Multipart
@POST("링크")
fun voiceSend(
@Header("Authorization") accessToken: String,
@Part("member") member: RequestBody,
@Part("question") question: RequestBody,
@Part record: MultipartBody.Part
) : Call<VoiceResponse>
}
Service
class VoiceService {
val retrofit = Retrofit.Builder()
.baseUrl("기본 링크")
.addConverterFactory(GsonConverterFactory.create())
.build()
private val voiceService = retrofit.create(VoiceInterface::class.java)
fun sendVoice(accessToken: String, member: RequestBody, question: RequestBody, record: MultipartBody.Part, callback: (VoiceResponse?) -> Unit) {
val call = voiceService.voiceSend("$accessToken", member, question, record)
call.enqueue(object : Callback<VoiceResponse> {
override fun onResponse(call: Call<VoiceResponse>, response: Response<VoiceResponse>) {
when (response.code()) {
200 -> {
callback(response.body())
}
401 -> {
callback(response.body())
}
500 -> {
callback(response.body())
}
4001 -> {
callback(response.body())
}
else -> {
callback(null)
}
}
}
override fun onFailure(call: Call<VoiceResponse>, t: Throwable) {
callback(null)
}
})
}
}
Activity/Fragment
val voiceService = VoiceService()
val memberRequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), it.snsId)
val questionRequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), binding.randomQTv.text.toString())
voiceService.sendVoice(loadedToken, memberRequestBody, questionRequestBody, recordPart) { voiceDTO ->
if (voiceDTO != null) {
if (voiceDTO.isSuccess.toString() == "true") {
}
}
}
'공부 > 안드로이드' 카테고리의 다른 글
[Android/Kotlin] 다른 Activity 종료 (0) | 2024.02.22 |
---|---|
[Android/Kotlin] 복사하기 기능 (0) | 2024.02.21 |
[Android/Kotlin] 음성 녹화 (0) | 2024.02.21 |
[Android/Kotlin] 음성 재생/일시정지 (0) | 2024.02.21 |
[Android/Kotlin] 카카오 로그인/회원가입 (0) | 2024.02.21 |