아래의 영상을 참고했습니다.
https://www.youtube.com/watch?v=oyKmeW2Kldc&list=PLSrm9z4zp4mF1Ssdfuocy2XH5Bw4wLLNw&index=5

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyInterceptor : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val request = chain.request() | |
.newBuilder() | |
.addHeader("Content-Type", "application/json") | |
.addHeader("X-Platform", "Android") | |
.addHeader("X-Auth-Token", "123456789") | |
.build() | |
return chain.proceed(request) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object RetrofitInstance { | |
private val client = OkHttpClient.Builder().apply { | |
addInterceptor(MyInterceptor()) | |
}.build() | |
private val retrofit by lazy { | |
Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.client(client) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
val api : SimpleApi by lazy { | |
retrofit.create(SimpleApi::class.java) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SimpleApi { | |
@GET("posts/1") | |
suspend fun getPost(@Header("Auth") auth: String): Response<Post> | |
@GET("posts/{postNumber}") | |
suspend fun getPost2( | |
@Path("postNumber") number : Int | |
) : Response<Post> | |
@GET("posts") | |
suspend fun getCustomPost( | |
@Query("userId") userId : Int, | |
@Query("_sort") sort : String, | |
@Query("_order") order : String | |
) : Response<List<Post>> | |
@GET("posts") | |
suspend fun getCustomPosts2( | |
@Query("userId") userId: Int, | |
@QueryMap options : Map<String, String> | |
) : Response<List<Post>> | |
@POST("posts") | |
suspend fun pushPost( | |
@Body post: Post | |
):Response<Post> | |
@FormUrlEncoded | |
@POST("posts") | |
suspend fun pushPost2( | |
@Field("userId") userId: Int, | |
@Field("id") id: Int, | |
@Field("title") title: String, | |
@Field("body") body: String, | |
) : Response<Post> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Repository { | |
suspend fun getPost(auth: String): Response<Post> { | |
return RetrofitInstance.api.getPost(auth) | |
} | |
suspend fun getPost2(number : Int) : Response<Post>{ | |
return RetrofitInstance.api.getPost2(number) | |
} | |
suspend fun getCustomPosts(userId : Int, sort : String, order : String) : Response<List<Post>> { | |
return RetrofitInstance.api.getCustomPost(userId, sort, order) | |
} | |
suspend fun getCustomPosts2(userId: Int, options : Map<String, String>) : Response<List<Post>> { | |
return RetrofitInstance.api.getCustomPosts2(userId, options) | |
} | |
suspend fun pushPost(post : Post) : Response<Post>{ | |
return RetrofitInstance.api.pushPost(post) | |
} | |
suspend fun pushPost2(userId : Int, id : Int, title : String, body : String) : Response<Post> { | |
return RetrofitInstance.api.pushPost2(userId, id, title, body) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var viewModel: MainViewModel | |
private val myAdapter by lazy { MyAdapter() } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setupRecyclerview() | |
val repository = Repository() | |
val viewModelFactory = MainViewModelFactory(repository) | |
viewModel = ViewModelProvider(this, viewModelFactory).get(MainViewModel::class.java) | |
// viewModel.getCustomPosts(2, "id", "desc") | |
viewModel.getPost("11112222") | |
viewModel.myResponse.observe(this, Observer { response -> | |
if(response.isSuccessful){ | |
Log.d("Main", response.body().toString()) | |
Log.d("Main", response.code().toString()) | |
Log.d("Main", response.headers().toString()) | |
}else { | |
Toast.makeText(this, response.code(), Toast.LENGTH_SHORT).show() | |
} | |
}) | |
viewModel.myCustomPosts.observe(this, Observer { response -> | |
if(response.isSuccessful) { | |
response.body()?.let { | |
myAdapter.setData(it) | |
} | |
} else { | |
Toast.makeText(this, response.code(), Toast.LENGTH_SHORT).show() | |
} | |
}) | |
} | |
private fun setupRecyclerview(){ | |
val recyclerview = findViewById<RecyclerView>(R.id.recyclerView) | |
recyclerview.adapter = myAdapter | |
recyclerview.layoutManager = LinearLayoutManager(this) | |
} | |
} |
'Android(Kotlin)' 카테고리의 다른 글
Android Studio Bumblebee classpath (0) | 2022.02.20 |
---|---|
Retrofit에 관하여 - Call/Response, GSON, OKHttp (0) | 2022.02.05 |
Retrofit - Send a simple POST Request | Android Studio Tutorial (0) | 2022.02.03 |
Retrofit - Display results in a RecyclerView | Android Studio Tutorial (0) | 2022.02.03 |
Retrofit - URL Manipulation with @Path @Query @QueryMap | Android Studio Tutorial (0) | 2022.02.02 |