I have the following code which makes an API call to get Addresses from Postcode.
fun getAddressFromPostCode(postCode: String): List<Address>{
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"}""")
val url = RequestConstants.URL
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
val api:GetAddressAPIService = retrofit.create(GetAddressAPIService ::class.java)
var myList = emptyList<Address>()
val myCall: Call<GetAddressResponse> = api.getAddress(body)
myCall.enqueue(object : Callback<GetAddressResponse> {
override fun onFailure(call: Call<GetAddressResponse>?, t: Throwable?) {
Log.d("RegistrationInteractor", "Something went wrong", t)
Log.d("RegistrationInteractor", call.toString())
}
override fun onResponse(call: Call<GetAddressResponse>?, response: Response<GetAddressResponse>?) {
// Success response
myList = response!!.body()!!.addresses
}
})
return myList
}
And here's where I make the call:
interface GetAddressAPIService {
@Headers("Accept: application/json; Content-Type: application/json")
@POST("postcode_search.php")
fun getAddress(@Body dataBody: JSONObject): Call<GetAddressResponse>
} The GetAddressResponse looks like this and seems to be correct: data class GetAddressResponse( val success: Int, val addresses: List )
The databody is {"data":{"postcode":"M130EN"},"data_signature":"long data signature"} and when I run this in Postman I get a list of addresses, but when I run it in the app I get a 200 OK response but no addresses. Any ideas why?
GetAddressResponse
. Does it has correct structure? – Nostradamus