GBG Developers

Guides   |   Using the Address Service (Archived Version v1.0)

Using the Address Service (Archived Version v1.0)

The address service can be used for purposes such as rapidly entering an address manually. A typical scenario would be to allow the mobile user to begin typing in the postcode, and a list of addresses would be displayed as they type. From the list of addresses, the user could select one and the SDK would then query the GBG Identity Solution to retrieve the full address details to fully populate all fields in a form.

The Address Service portion of the SDK therefore is used in two stages. The first stage can be used as the postcode is typed. Here’s an example where the postcode text to be queried is sent as a parameter in the findAddresses method.

First, create the addressService as shown here:

val addressService = AddressService(Config("https://api.addressy.com/"))

Next, pass the address in the findAddresses method. The returned result contains a list of possible addresses.

var addressId : String? = ""
val codepost="SW2 223T"
addressService.findAddresses(codepost, resultListener = object :
    VerifyResultListener<Response<List<FindAddressResponse>>> {
    override fun success(response: Response<List<FindAddressResponse>>) {
        val addrentry=response?.body()?.get(0)
        addressId=addrentry?.id
        Log.d(TAG, "findAddresses successful response: $response ")
        Log.d(TAG, "identifier: $addressId")
    }
    override fun failure(error: ErrorResponse) {
        Log.e(TAG, “findAddresses error: ${error.responseCode}")
    }
})

The list of addresses contains the following parameters per entry. The text and description parameters per entry could be used to create a list of options for the user to tap to select a list entry. The code snippet above displays the content of the id parameter for the first entry in the returned list.

Name Example
id GB|RM|B|88888888
type Address
text 222, Baker Street
highlight  
description London, SW2 223T

 

The screenshot here shows an example mobile application where the user has begun typing in a postcode, and the first stage of the Address Service was executed. The returned list of addresses consisting of a concatenation of the text and description parameters, is listed for the user to select from.



Once the user has chosen one of the entries, the second stage of the Address Service can be used. This second stage will submit the chosen id value to the GBG Identity Solution and return the complete address.

addressService.retrieve(RetrieveAddressRequest(addressId!!), resultListener = object :
    VerifyResultListener<Response<List<RetrieveAddressResponse>>> {
    override fun success (response: Response < List < RetrieveAddressResponse >>) {
        val addrfull=response?.body()?.get(0)
        val textfield=addrfull?.address
        Log.d(TAG, "address retrieve Successful response: $response " )
        Log.d(TAG, "textfield: $textfield")
    }
    override fun failure (error: ErrorResponse ) {
        Log.e(TAG, "address retrieve Error: ${error.responseCode}" )
    }
})

The example above will just print out the address field from the response, however far granular detail is returned. The other values include the building information, administrative area and locality for instance. You can use Android Studio’s Control-B functionality to examine the field names when RetrieveAddressResponse is highlighted in the code above.