Android(Kotlin)

Retrofit - Custom Request Headers | Android Studio Tutorial

----___<<<<< 2022. 2. 4. 01:09

아래의 영상을 참고했습니다.

https://www.youtube.com/watch?v=oyKmeW2Kldc&list=PLSrm9z4zp4mF1Ssdfuocy2XH5Bw4wLLNw&index=5 

 

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)
}
}
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)
}
}
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>
}
view raw SimpleApi.kt hosted with ❤ by GitHub
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)
}
}
view raw Repository.kt hosted with ❤ by GitHub
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)
}
}
view raw MainActivity.kt hosted with ❤ by GitHub