Android Kotlin : Retrofit2 동적 Url주소 만들기

2022. 9. 8. 09:15[Android APP] feat. Kotlin/Kotlin 공부

개요

Retrofit으로 통신을 하기 위해 코드를 작성 중에

고정 Url이 아닌 사용자마다 주소가 바뀌는 Uri를 넣어줘야하는 경우가 발생했다.

 

본문

@Path 이노테이션 사용하기

 

1. Interface

interface chkUserIdService {
    @Headers("Content-Type: application/json")
    @GET("/user/checkUId/{userId}")
    fun requestchkUserId(
        @Path("userId") userId : String,
        @Header("code") code: String?
    ): Call<checkId>
}

서버 uri값이 /user/checkId/{userID}처럼 사용자의 아이디 값을 동적으로 넣어줘야하는 경우같을 때

@Path를 사용해서 주소에 userId라는 변수 값을 넣어주면 된다

 

2. Activity

Activity에 Retrofit을 호출 할 때 순서대로 값을 넣어주면 된다.

반응형