I'm using Retrofit2 for the first time, so I'm confused how to proceed. I have the following code
fun getAddressFromPostCode(postCode: String): List<PXAddress>{
val trimmedPostCode = postCode.replace("\\s".toRegex(),"").trim()
val dataBody = JSONObject("""{"postCode":"$trimmedPostCode"}""").toString()
val hmac = HMAC()
val hmacResult = hmac.sign(RequestConstants.CSSecretKey, dataBody)
val body = JSONObject("""{"data":"$dataBody", "data_signature":"$hmacResult"}""").toString()
val url = RequestConstants.getAddress
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
val address: PXAddress = retrofit.create(PXAddress::class.java)
}
with the idea that body needs to look like this:
"data":{ "postcode": "WA1 1LD" }, "data_signature": "{{getSignature}}" }
and the response should be
"success": 1, "addresses": [ { "address1": "47 Museum Street", "address2": null, "address3": null, "town": "WARRINGTON", "county": "", "postcode": "WA1 1LD" }, { "address1": "49a Museum Street", "address2": null, "address3": null, "town": "WARRINGTON", "county": "", "postcode": "WA1 1LD" }, { "address1": "Leda Recruitment", "address2": "49 Museum Street", "address3": null, "town": "WARRINGTON", "county": "", "postcode": "WA1 1LD" } ] }
And I need to convert that response into a list of PXAddress which is
open class PXAddress : RealmObject() {
var addressLine1: String? = null
var addressLine2: String? = null
var addressLine3: String? = null
var town: String? = null
var county: String? = null
var postcode: String? = null
}