와챠의 우당탕탕 코딩 일기장

[Android/Kotlin]Kakao Address api 사용해보기 본문

코딩 일기장/Android(Kotlin)

[Android/Kotlin]Kakao Address api 사용해보기

minWachya 2022. 10. 10. 14:20
반응형

최종 구현 ui

 

Kakao address api를 사용해 위 화면을 만들어 볼 것입니다.

위 화면의 기능들을 소개하면 아래와 같습니다.

 

1. 검색한 키워드가 들어가는 주소 보이기

2. 상세1, 상세2, 상세3 주소까지 보이기(서울, 영등포구, 양평동)

3. RecyclerView의 item(주소)을 클릭하면 액티비티가 그 주소값을 반환하며 finish


먼저 API에 대해 간단히 알아봅시다.

이 링크는 주소 검색하기 Kakao 공식 문서입니다.

 

request는 아래와 같습니다.

1. GET 사용

2. base url은 https://dapi.kakao.com/

3. 주소 검색 시 추가 url은 v2/local/search/address.json

4. parameta들은 아래와 같음

Name Type Description Resuired
query String 검색을 원하는 질의어 O
analyze_type String 검색 결과 제공 방식
다음 중 하나:
similar: 입력한 건물명과 일부만 매칭될 경우에도 확장된 검색 결과 제공, 미지정 시 similar가 적용됨
exact: 주소의 정확한 건물명이 입력된 주소패턴일 경우에 한해, 입력한 건물명과 정확히 일치하는 검색 결과 제공
(기본값: similar)
X
page Integer 결과 페이지 번호
(최소: 1, 최대: 45, 기본값: 1)
X
size Integer 한 페이지에 보여질 문서의 개수
(최소: 1, 최대: 30, 기본값: 10)
X

 

response는 아래와 같습니다.

{
  "meta": {
    "total_count": 4,
    "pageable_count": 4,
    "is_end": true
  },
  "documents": [
    {
      "address_name": "전북 익산시 부송동 100",
      "y": "35.97664845766847",
      "x": "126.99597295767953",
      "address_type": "REGION_ADDR",
      "address": {
        "address_name": "전북 익산시 부송동 100",
        "region_1depth_name": "전북",
        "region_2depth_name": "익산시",
        "region_3depth_name": "부송동",
        "region_3depth_h_name": "삼성동",
        "h_code": "4514069000",
        "b_code": "4514013400",
        "mountain_yn": "N",
        "main_address_no": "100",
        "sub_address_no": "",
        "x": "126.99597295767953",
        "y": "35.97664845766847"
      },
      "road_address": {
        "address_name": "전북 익산시 망산길 11-17",
        "region_1depth_name": "전북",
        "region_2depth_name": "익산시",
        "region_3depth_name": "부송동",
        "road_name": "망산길",
        "underground_yn": "N",
        "main_building_no": "11",
        "sub_building_no": "17",
        "building_name": "",
        "zone_no": "54547",
        "y": "35.976749396987046",
        "x": "126.99599512792346"
      }
    },
    ...
  ]
}

제가 필요한 정보는

documents>address>region_1depth_name, region_2depth_name, region_3depth_name입니다.


이제 직접 코드로 짜봅시다^_^

 

0. 필요 라이브러리들 선언

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    ...
    dataBinding {
        enabled = true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation("androidx.collection:collection-ktx:1.2.0") // by viewModels()

    // Hilt
    implementation "com.google.dagger:hilt-android:2.42"
    kapt "com.google.dagger:hilt-android-compiler:2.42"

    // Network
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation platform("com.squareup.okhttp3:okhttp-bom:4.9.0")
    implementation "com.squareup.okhttp3:okhttp"
    implementation "com.squareup.okhttp3:logging-interceptor"

	...

}

 

1. di/RetrofitModule.kt

서버 연결을 위해 retrofit을 만들어줍니다.

base_url은 위에서 본 https://dapi.kakao.com/를 써줍니다.

저는 다른 서버 연결을 위한 retrofit도 필요했기 때문에 @Named를 사용해 구분해주었습니다.

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object RetrofitModule {
    private const val ADDRESS_URL = "https://dapi.kakao.com/"

    @Singleton
    @Provides
    fun providesOkHttpClient(): OkHttpClient =
        OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build()

    @Provides
    @Named("Address")
    @Singleton
    fun providesAddressRetrofit(okHttpClient: OkHttpClient): Retrofit =
        Retrofit.Builder()
            .baseUrl(ADDRESS_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
}

 

2. data/remote/model/response/AddressResponse.kt

Response를 받을 data class를 생성해줍니다.

제가 필요한 부분들만 만들어줬습니다.

data class AddressResponse(
    val documents: List<Address>
)

data class Address(
    @SerializedName("address") val addressInfo: AddressInfo,
)

data class AddressInfo(
    @SerializedName("region_1depth_name") val address1: String,
    @SerializedName("region_2depth_name") val address2: String,
    @SerializedName("region_3depth_name") val address3: String
)

 

3. data/remote/service/AddressService.kt

Kakao address api와 연결할 서비스를 생성합니다.

GET을 사용하고 v2/local/search/address.json url로 연결해줍니다.

파라미터들도 필요한 부분만 써줬습니다.

interface AddressService {
    @GET("v2/local/search/address.json")
    suspend fun getAddressList(@Query("query") query: String,
                               @Query("page") page: Int,
                               @Query("size") size: Int,
                               @Header("Authorization") apikey: String)
    : AddressResponse
}

 

4. di/RetrofitServiceModule.kt

ServiceModule을 만들어줍니다.

@Module
@InstallIn(SingletonComponent::class)
object RetrofitServiceModule {
    @Provides
    @Singleton
    fun provideAddressService(@Named("Address") retrofit: Retrofit): AddressService =
        retrofit.create(AddressService::class.java)
}

 

5. data/remote/datasource/AddressDataSource.kt, AddressDataSourceImpl.kt

DataSource를 Interface를 만들고, 이를 상속받은 객체를 만들어줍니다.

interface AddressDataSource {
    suspend fun getAddressList(query: String,
                               page: Int,
                               size: Int,
                               apikey: String): AddressResponse
}

class AddressDataSourceImpl @Inject constructor(private val addressService: AddressService): AddressDataSource {
    override suspend fun getAddressList(
        query: String,
        page: Int,
        size: Int,
        apikey: String
    ): AddressResponse = addressService.getAddressList(query, page, size, apikey)

}

 

6. di/DataSourceModule.kt

DataSourceModule을 만들어줍니다.

@Module
@InstallIn(SingletonComponent::class)
interface DataSourceModule {

    @Binds
    @Singleton
    fun bindsAddressDataSource(dataSourceImpl: AddressDataSourceImpl): AddressDataSource
}

 

7. data/remote/repository/AddressRepository.kt, AddressRepositoryImpl.kt

Repository interface를 생성하고, 이를 상속받은 객체를 생성합니다.

interface AddressRepository {
    suspend fun getAddressList(query: String,
                               page: Int,
                               size: Int,
                               apikey: String): AddressResponse
}

class AddressRepositoryImpl @Inject constructor(
                            private val dataSource: AddressDataSource
): AddressRepository {
    override suspend fun getAddressList(query: String,
                               page: Int,
                               size: Int,
                               apikey: String): AddressResponse
    = dataSource.getAddressList(query, page, size, apikey)
}

 

8. di/RepositoryModule.kt

RepositoryModule도 만들어줍니다~ Hilt가 AddressRepository를 잘 찾도록요.

@Module
@InstallIn(SingletonComponent::class)
interface RepositoryModule {

    @Binds
    @Singleton
    fun bindsAddressRepository(repository: AddressRepositoryImpl): AddressRepository
}

여기까지 서버에 주소 정보를 요청할 준비가 끝났습니다.

이제 직접 요청을 하고, 받은 데이터를 처리해 보여주면 됩니다.

 

이제 xml을 만들어보겠습니다.


9. activity_search_address.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".address_search.SearchAddressActivity">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ToolbarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:title="@string/address_search_toolbar_title"
            app:titleTextAppearance="@style/Toolbar.TitleText" />

        <EditText
            android:id="@+id/et_address"
            android:layout_width="0dp"
            android:layout_height="54dp"
            android:layout_marginStart="20dp"
            android:background="@null"
            android:hint="@string/search_hint"
            android:imeOptions="actionSearch"
            android:inputType="text"
            app:layout_constraintEnd_toStartOf="@id/btn_delete"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tool_bar" />

        <ImageButton
            android:id="@+id/btn_delete"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/ic_delete"
            app:layout_constraintBottom_toBottomOf="@id/btn_search"
            app:layout_constraintEnd_toStartOf="@id/btn_search"
            app:layout_constraintTop_toTopOf="@id/btn_search" />

        <Button
            android:id="@+id/btn_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/transparency"
            android:text="@string/search"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@id/et_address"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/et_address" />

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/et_address" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_address"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/view"
            tools:listitem="@layout/item_address_add" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

 

10. item_address_add.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="address"
            type="com.example.dongnaegoyang.data.local.Address" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/iv_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="22dp"
            android:src="@drawable/ic_location_pin"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_address1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{address.addressInfo.address1}"
            android:textColor="@color/black_473A22"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="@+id/iv_location"
            app:layout_constraintStart_toEndOf="@id/iv_location"
            app:layout_constraintTop_toTopOf="@+id/iv_location"
            tools:text="경기도" />

        <TextView
            android:id="@+id/tv_arrow1"
            isExistAddress="@{address.addressInfo.address2}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="&gt;"
            android:textColor="@color/gray_BBB5A4"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="@id/tv_address1"
            app:layout_constraintStart_toEndOf="@+id/tv_address1"
            app:layout_constraintTop_toTopOf="@+id/tv_address1" />

        <TextView
            android:id="@+id/tv_address2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@{address.addressInfo.address2}"
            android:textColor="@color/black_473A22"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="@id/tv_arrow1"
            app:layout_constraintStart_toEndOf="@id/tv_arrow1"
            app:layout_constraintTop_toTopOf="@+id/tv_arrow1"
            tools:text="경기도" />

        <TextView
            android:id="@+id/tv_arrow2"
            isExistAddress="@{address.addressInfo.address3}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="&gt;"
            android:textColor="@color/gray_BBB5A4"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="@id/tv_address2"
            app:layout_constraintStart_toEndOf="@+id/tv_address2"
            app:layout_constraintTop_toTopOf="@+id/tv_address2" />

        <TextView
            android:id="@+id/tv_address3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@{address.addressInfo.address3}"
            android:textColor="@color/black_473A22"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="@+id/tv_arrow2"
            app:layout_constraintStart_toEndOf="@id/tv_arrow2"
            app:layout_constraintTop_toTopOf="@+id/tv_arrow2"
            tools:text="경기도" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/gray_F6F3E9"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

 

11. ui/search_address_add/SearchAddressViewModel.kt

ViewModel을 만들어줍니다.

여기서 직접 address data를 부르고, 받아오는 함수를 만듭니다.

@HiltViewModel
class SearchAddressViewModel @Inject constructor(
    private val repository: AddressRepository
): ViewModel() {
    private val _addressResponse = MutableLiveData<AddressResponse>()
    val addressResponse: LiveData<AddressResponse> = _addressResponse

    fun getAddressListResponse(query: String,
                               page: Int,
                               size: Int,
                               apikey: String) = viewModelScope.launch {
        kotlin.runCatching {
            repository.getAddressList(query, page, size, apikey)
        }.onSuccess {
            _addressResponse.value = it
        }.onFailure {
            Log.d("mmm", "get address add api fail ${it.message}")
        }
    }

}

 

12.ui/search_address_add/SelectAddressInterface.kt, AddressAdapter.kt

RecyclerView의 item(주소)을 클릭하면 액티비티가 그 주소값을 반환하며 finish되는 로직을 만드려고 합니다.

이를 위해 SelectAddressInterface를 생성하고, AddressAdapter에 이 인터페이스를 상속받는 객체를 넣어줍니다.

그리고 한 item을 클릭했을 때, 이 인터페이스를 사용해 주소값을 반환하도록 코드를 짰습니다.

interface SelectAddressInterface {
    fun onSelectedAddress(address1: String, address2: String, address3: String)
}

class AddressAdapter(val context: Context) :
    ListAdapter<Address, AddressAdapter.AddressViewHolder>(AddressDiffCallback()) {
    private lateinit var binding: ItemAddressAddBinding

    // Adapter 값 리턴하기 위한 인터페이스 객체
    private lateinit var mCallback: SelectAddressInterface

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddressViewHolder {
        mCallback = context as SelectAddressInterface

        binding = ItemAddressAddBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return AddressViewHolder(binding)
    }

    override fun onBindViewHolder(holder: AddressViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class AddressViewHolder(private val binding: ItemAddressAddBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(address: Address) {
            binding.address = address
            binding.executePendingBindings()

            binding.constraintLayout.setOnClickListener {
                // 리턴할 값 넣기!!
                mCallback.onSelectedAddress(address.addressInfo.address1,
                    address.addressInfo.address2,
                    address.addressInfo.address3)
            }
        }
    }

}

class AddressDiffCallback : DiffUtil.ItemCallback<Address>() {
    override fun areItemsTheSame(oldItem: Address, newItem: Address): Boolean {
        return oldItem.addressName == newItem.addressName
    }

    override fun areContentsTheSame(oldItem: Address, newItem: Address): Boolean {
        return oldItem == newItem
    }
}

 

13. ui/search_address_add/SearchAddressActivity.kt

이제 Activity 코드입니다.

검색 버튼을 누르면 해당 검색어를 quary로 하여 address를 받아와서,

해당 결과를 adapter에 넣어줍니다.

rest api key 부분엔 자신의 key를 써주심 됩니다.

노출되지 않기를 원한다면 아래 포스트를 참고하세요.

https://min-wachya.tistory.com/220

 

[Android/Kotlin] hide KEY/KEY 숨기기

ACCESS KEY나 SECRET ACCESS KEY 같이 중요한 정보는 github에서 보여지면 위험하기 때문에 꼬옥 숨겨주어야 한다. 프로젝트 할 때 S3와 통신할 일이 있어서 ACCESS KEY랑 SECRET ACCESS KEY를 안드 내에 저장하고..

min-wachya.tistory.com

 

이 Activity는 SelectAddressInterface를 상속합니다.

따라서 RecyclerView에서 item을 선택했을 때,

그 item의 주소들을 받아 finish하는 로직을 onSelectedAddress 함수 내에서 구현할 수 있습니다.

@AndroidEntryPoint
class SearchAddressActivity :
    BaseActivity<ActivitySearchAddressAddBinding>(R.layout.activity_search_address_add),
    SelectAddressInterface{
    private val viewModel: SearchAddressViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setToolbar()
        searchAddress()
        setObserveAddress()
    }

    // 툴바 달기
    private fun setToolbar() {
        setSupportActionBar(binding.toolBar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    private fun searchAddress() {
        binding.btnSearch.setOnClickListener {
            val keyword = binding.etAddress.text.toString()
            if(keyword.isEmpty()){
                Toast.makeText(this, "주소를 입력해 주세요.", Toast.LENGTH_SHORT).show()
            }else {
                viewModel.getAddressListResponse(keyword, 1, 10, "rest api key")
            }
        }
    }

    private fun setObserveAddress() {
        viewModel.addressResponse.observe(this) { response ->
            binding.rvAddress.adapter =
                AddressAdapter(this).apply {
                    submitList(response.documents)
                }
        }

    }

    // 툴바에서 뒤로가기 버튼 클릭 시
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onSelectedAddress(address1: String, address2: String, address3: String) {
        val intent = Intent()
        intent.putExtra("address1", address1)
        intent.putExtra("address2", address2)
        intent.putExtra("address3", address3)
        setResult(RESULT_OK, intent)
        finish()
    }
}

 

14. SearchAddressActivity를 호출할 땐 아래의 코드를 사용합니다.

binding.tvTown.setOnClickListener {
    startForResult.launch(Intent(requireContext(), SearchAddressActivity::class.java))
}

// 주소 검색 Activity 이동 후 결과
    private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent = result.data
            sido = intent?.getStringExtra("address1").toString()
            gugun = intent?.getStringExtra("address2").toString()
            dong = intent?.getStringExtra("address3").toString()
            binding.tvTown.text = if("$sido $gugun $dong".trim() == "") null else "$sido $gugun $dong".trim()
            checkTown = true
            btnEnableCheck()
        }
    }

끝~~~ 생각보다 간단하죠?

이제 api 관련 코드는 금방금방 짭니다,,프하학

반응형
Comments