我使用Retrofit2调用了Web服务,但是未调用onResponse且调用了onFailure尽管服务的操作在服务器端已成功完成
每当我尝试使用fiddler来检查服务的工作情况时,我发现问题是序列化了服务的即将到来的响应,而当使用fiddler时,我发现JSON响应中没有内容,因此Retrofit服务假定其失败是因为没有内容,它不能序列化EMPTY内容,并给我这个错误
请求
{
"mode":"JainSanghViewMember",
"reg_par_id":"11" }
回复
{
"Status": 1,
"userDetails":[
{
"reg_fm_id": 27,
"reg_fm_name": "Hitesh Bhatiya",
"reg_fm_mobileno": "9913990087",
"reg_fm_email": "hitesh.vnurture@gmail.com",
"reg_fm_profilepic": "",
"relationship_name": "Self"
},
{
"reg_fm_id": 64,
"reg_fm_name": "Jayesh Pansheriya ",
"reg_fm_mobileno": "8905982801",
"reg_fm_email": "jayesh.vnurture@gmail.com",
"reg_fm_profilepic": "",
"relationship_name": "Brother"
}
],
"Message": "Family member details listed successfully"
}
我的代码是
interface APIInterface {
@FormUrlEncoded
@POST("webservice.php")
fun viewUser(@Field("mode") mode: String,@Field("reg_par_id") reg_par_id:String): Call<UserList>
}
object RetrofitClient {
var retrofit: Retrofit? = null
fun getClient(baseUrl: String): Retrofit? {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
apiInterface = RetrofitClient.getClient(BASE_URL)!!.create(APIInterface::class.java)
var hero: Call<UserList> = apiInterface!!.viewUser("JainSanghViewMember","11")
hero.enqueue(object : Callback<UserList> {
override fun onFailure(call: Call<UserList>?, t: Throwable?) {
var t=Toast.makeText(this@MainActivity,"h"+t.toString(),Toast.LENGTH_LONG).show()
Log.i("jjjj",t.toString())
}
override fun onResponse(call: Call<UserList>?, response: Response<UserList>?) {
Log.d("hello", response!!.body().Status.toString())
var t=Toast.makeText(this@MainActivity,"h"+response!!.body().Status.toString(),Toast.LENGTH_LONG).show()
}
})
我的模型课程是:
data class UserList(
val Message: String,
val Status: Int,
val userDetails: List<UserDetail>
)
data class UserDetail(
val reg_fm_email: String,
val reg_fm_id: Int,
val reg_fm_mobileno: String,
val reg_fm_name: String,
val reg_fm_profilepic: String,
val relationship_name: String
)
请分享您的想法以解决此问题/错误 谢谢
答案 0 :(得分:0)
问题出在用户详细信息的数据类中,您可以在其中更改属性名称
reg_fm_mobileno
到
reg_fm_number
...,并且您已定义了所有属性(不为null),因此gson解析将失败,因为它无法在响应中找到reg_fm_number
。