Java I';m在进行请求后改装时获得空值
我对在Kotlin中使用API是相当陌生的,我正在使用改装库,我遇到了这个问题 发出post请求后获取空值 我试图得到这样的回应:Java I';m在进行请求后改装时获得空值,java,android,kotlin,retrofit,okhttp,Java,Android,Kotlin,Retrofit,Okhttp,我对在Kotlin中使用API是相当陌生的,我正在使用改装库,我遇到了这个问题 发出post请求后获取空值 我试图得到这样的回应: { "status": "success", "message": "Transfer Queued Successfully", "data": { "account_number": "0690000040&quo
{
"status": "success",
"message": "Transfer Queued Successfully",
"data": {
"account_number": "0690000040",
"bank_code": "044",
"currency": "NGN",
"amount": 5500
}
}
我的模型课
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)
}
})
}
}
数据类传输(
@序列化名称(“账户银行”)val accountBank:字符串?,
@SerializedName(“帐号”)val accountNumber:字符串?,
@序列化名称(“金额”)val金额:Int?,
@SerializedName(“货币”)val货币:字符串?
)
我的API接口
<?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>
接口传输API{
@标题(“内容类型:application/json”)
@员额(“调动”)
趣味createTransfer(@Body transfer:transfer):呼叫
}
XML文件
<?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>
我想提出以上的回应。您的帮助将不胜感激 看起来您的
传输
类并不代表实际响应。你需要一个包装,类似这样:
data class TransferResponse(
val status: String,
val message: String,
val data: Transfer
)
然后,您需要更改API接口以返回此包装器。看起来您的
传输
类并不代表实际响应。你需要一个包装,类似这样:
data class TransferResponse(
val status: String,
val message: String,
val data: Transfer
)
然后,您需要更改API接口以返回此包装器