๐ ์ง๋ ๊ฒ์ ๊ด๋ จ ๋ฌธ์: https://developers.naver.com/docs/serviceapi/search/local/local.md
โ์ฃผ์
๋ค์ด๋ฒ ์ง๋ api๋ฅผ ํตํด ๊ฒ์ํ๋ ค๋ฉด ๋ค์ด๋ฒ ์ง๋ api๋ง ์ ์ฒญํ๋ ๊ฒ ์๋๋ผ ๋ฐ๋ก ๊ฒ์ api๋ ์ ์ฒญํด์ผ ํ๋ค.
์ฒ์์ ์ด๊ฑธ ๋ชจ๋ฅด๊ณ ๊ตฌํํ๋ค๊ฐ ํ์ฐธ์ ํค๋งธ์;
API ์ ์ฒญ
1. Naver Developers์์ ์ ํ๋ฆฌ์ผ์ด์ ๋ฑ๋ก
https://developers.naver.com/apps/#/register
2. ์ฌ์ฉ API์์ ๊ฒ์ API ์ ํ
3. ๋ฑ๋ก
์ฝํ๋ฆฐ ํ์ผ ์์ฑ
1. data class
์ ๋ณด ์ ์ฅ ์ํ ํด๋์ค
data class Place(
@SerializedName("title") val title: String,
@SerializedName("description") val description: String,
@SerializedName("address") val address: String,
@SerializedName("roadAddress") val roadAddress: String
)
2. DTO
JSON์ผ๋ก api ์์ฒญ ์ ๋ฐํ ๋ฐ์์ ๋ items์ ๋ด๊ฒจ์๋ ์ ๋ณด๋ฅผ ๋ฐ์์ค๊ธฐ ์ํจ
data class PlaceListDTO (
@SerializedName("items") val places: List<Place>
)
3. ์ธํฐํ์ด์ค
๋งจ์ฒ์ ๋งํฌ ์ฒจ๋ถํ ๋ฌธ์ ์ฐธ๊ณ ํด์ ๋ณธ์ธ์ด ํ์ํ ๋ด์ฉ ์์ฑํ๋ฉด ๋จ
JSON์ผ๋ก ๋ฐํ๋ฐ๋ url ์ฌ์ฉํ์
ํด๋ผ์ด์ธํธ ์์ด๋์ ์ํฌ๋ฆฟ์ ๊ฒ์ API ์ ์ฒญํ๋ฉด ๋์ค๋ ๊ฑฐ ์ฌ์ฉ, Header ๊ฐ์ผ๋ก ๋๊ฒจ์ค๋ค
โResponse๋ก ๋ฐ์์ฌ ๋ suspend ํจ์ ์ฌ์ฉ
interface NaverMapService {
// ๋ค์ด๋ฒ ์ง๋ ์ฅ์ ๊ฒ์ api
@GET("/v1/search/local.json")
suspend fun getMapSearch(
@Query("query") query: String, // ๊ฒ์์ด
@Query("display") display: Int = 5, // ๊ฒ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ ๊ฑด์
@Header("X-Naver-Client-Id") clientId: String = "๋ด ํด๋ผ์ด์ธํธ ์์ด๋", // ๊ฒ์ ํด๋ผ์ด์ธํธ ์์ด๋
@Header("X-Naver-Client-Secret") clientSecret: String = "๋ด ํด๋ผ์ด์ธํธ ์ํฌ๋ฆฟ", // ๊ฒ์ ํด๋ผ์ด์ธํธ ์ํฌ๋ฆฟ
): Response<PlaceListDTO>
}
4. retrofit2 ์ค์
private val gson = GsonBuilder().setLenient().create()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://openapi.naver.com")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val mapService = retrofit.create(NaverMapService::class.java)
5. Activity ์ฝ๋
โsuspend ํจ์ ์ฌ์ฉ ์ lifecycleScope ์ฌ์ฉ
class PlaceSearchActivity : AppCompatActivity() {
// 4. retrofit2 ์ค์ ๋ถ๋ถ
private val binding by lazy { ActivityPlaceSearchBinding.inflate(layoutInflater) }
private val gson = GsonBuilder().setLenient().create()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://openapi.naver.com")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val mapService = retrofit.create(NaverMapService::class.java)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.recyclerviewSearchPlace.layoutManager = LinearLayoutManager(this)
setSearchView() // ๊ฒ์์ด ์
๋ ฅํ๋ ๋ถ๋ถ ์ค์
setContentView(binding.root)
}
private fun setSearchView() {
// ๊ฒ์์ด ์
๋ ฅ ๋ถ๋ถ
binding.searchViewPlace.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
lifecycleScope.launch {
// ๊ฐ ๋ฐ์์ค๊ธฐ
val mapSearchList = mapService.getMapSearch(query!!)
binding.recyclerviewSearchPlace.adapter = PlaceSearchAdapter(mapSearchList.body()!!.places)
binding.recyclerviewSearchPlace.isVisible = true
}
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
}
}
'Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android/Kotlin] ๊ตฌ๊ธ ๋ก๊ทธ์ธ ๊ตฌํ (0) | 2024.02.12 |
---|---|
[Android/Kotlin] ์๋ผ๋ API ์ฐ๋ (1) | 2024.02.12 |
[Android/Kotlin] ๋ค์ด๋ฒ ์ง๋ API ์ฐ๋ (0) | 2024.02.06 |
๋๊ธ