Reputation: 3
I'm fairly new to consuming API in Kotlin, I'm using Retrofit library and i ran into this problem Getting null values after making post request.
I'm trying to get this response :
{
"status": "success",
"message": "Transfer Queued Successfully",
"data": {
"account_number": "0690000040",
"bank_code": "044",
"currency": "NGN",
"amount": 5500
}
}
My Model class
data class Transfer (
@SerializedName("account_bank") val accountBank: String?,
@SerializedName("account_number") val accountNumber: String?,
@SerializedName("amount") val amount: Int?,
@SerializedName("currency") val currency: String?
)
My API interface
interface TransferApi {
@Headers("Content-Type: application/json")
@POST("transfers")
fun createTransfer(@Body transfer: Transfer) : Call<Transfer>
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/responseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity Class
class MainActivity : AppCompatActivity() {
private lateinit var transferApi: TransferApi
private val bankInput = "044"
private val numberInput = "0690000040"
private val amountInput = 500
private val currency = "NGN"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val secret = "SECRET_KEY"
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(OkHttpClient.Builder().addInterceptor { chain ->
val request = chain.request().newBuilder().addHeader("Authorization", "Bearer $secret").build()
chain.proceed(request)
}.build())
.build()
transferApi = retrofit.create(TransferApi::class.java)
createTransfer()
}
private fun createTransfer(){
val transferCode = Transfer(bankInput,numberInput,amountInput,currency)
val call = transferApi.createTransfer(transferCode)
call.enqueue(object : Callback<Transfer> {
@SuppressLint("SetTextI18n")
override fun onResponse(call: Call<Transfer>, response: Response<Transfer>) {
if (!response.isSuccessful) {
responseText.text ="Code${response.code()}"
return
}
responseText.text = response.body().toString()
}
override fun onFailure(call: Call<Transfer>, t: Throwable) {
responseText.text = (t.message)
}
})
}
}
Response i'm getting
{
accountBank = null,
accoutNumber = null,
amount = null,
currency = null
}
I want to proposed response above.Your help would be greatly appreciated!!!
Upvotes: 0
Views: 398
Reputation: 164
It looks like your Transfer
class doesn't represent the actual response. You'll need a wrapper around it, something like this:
data class TransferResponse(
val status: String,
val message: String,
val data: Transfer
)
And after that you need to change your API interface to return this wrapper.
Upvotes: 2