本文介绍了改造错误预期为BEGIN_ARRAY,但为BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用手机号码获取Otp,但是它显示出这样的错误
I try to get Otp using mobile number, but it display error like this
这是我的网址:= http://192.168.1.105/XXXX /XXXXX/XXXXX/default/send-otp
响应是这样的:-
{
"error": false,
"msg": "Otp sent successfully",
"data": {
"otp": 152265
}
}
APIClient.Kt:-
object ApiClient {
private var retrofit: Retrofit? = null
val client: Retrofit
get() {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
APIInterface.kt:-
interface ApiInterface {
@FormUrlEncoded
@POST("send-otp")
fun GET_OTP(@Field("name") name: String, @Field("mobileNo") mobileNo: String): Call<OTPSendResponse>
}
AppConfig.kt:-
class AppConfig {
companion object {
const val BASE_URL = "http://192.168.1.105/XXXX/XXXXX/XXXXX/default/"
}
}
OtpModel.kt:-
class OtpModel {
constructor(otp: Int) {
this.otp = otp
}
@SerializedName("otp")
var otp: Int = 0
}
OtpSendResponse.kt:-
class OTPSendResponse {
constructor(error: String, data: ArrayList<OtpModel>, msg: String) {
this.error = error
this.data = data
this.msg = msg
}
@SerializedName("error")
var error: String = ""
@SerializedName("msg")
var msg: String = ""
@SerializedName("data")
var data: ArrayList<OtpModel> = ArrayList()
}
MyActivity.kt:-
private fun sendNameAndMobileNum(name: String, mobileNum: String) {
Log.e("MOBILE", "${mobileNum}")
val apiService = ApiClient.client.create(ApiInterface::class.java)
val call = apiService.GET_OTP(name, mobileNum)
call.enqueue(object : Callback<OTPSendResponse> {
override fun onResponse(call: Call<OTPSendResponse>, response: Response<OTPSendResponse>) {
Log.e("OTP", "${response.body()?.data!![0].otp}")
val otpIs = response.body()!!.data[0].otp
val i = Intent(this@AddNumActivity, OTPVerifyActivity::class.java)
i.putExtra("otp", otpIs)
i.putExtra("mobileNum", mobileNum)
startActivity(i)
}
override fun onFailure(call: Call<OTPSendResponse>, t: Throwable) {
Toast.makeText(this@AddNumActivity, "Ooops !!", Toast.LENGTH_SHORT).show()
Log.e("FAILISJERE", "${t.message}")
}
})
}
推荐答案
更改模型类,因为在json响应中没有任何数组,因此请删除ArrayList标记
Change Model class because in json response there are not any array so remove ArrayList tag
data: ArrayList<OtpModel>
到
data: OtpModel
因为它没有数组
这篇关于改造错误预期为BEGIN_ARRAY,但为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!