即使正确响应,仍会调用翻新的onFailure方法

时间:2018-08-28 12:31:04

标签: android kotlin gson retrofit2

以下是我在PostMan中的请求参数

{"assign_id":"1","type":2,"attendance_list":[{"stud_id":"1703","attendanceID":"1","stud_attendance":"4"},{"stud_id":"1704","attendanceID":"2","stud_attendance":"1"},{"stud_id":"1705","attendanceID":"3","stud_attendance":"1"},{"stud_id":"1706","attendanceID":"4","stud_attendance":"1"},{"stud_id":"1707","attendanceID":"5","stud_attendance":"1"},{"stud_id":"1727","attendanceID":"25","stud_attendance":"1"}]}

以下是响应

{"status":1,"msg":"Success"}

现在在我的Android应用中,我正在使用Gson进行翻新。但是通过Gson时,我遇到了一些问题,因此我以jsonObjectjsonArrays的形式发送请求参数。

下面是我的代码,当按下按钮向服务器提交请求时

val jObjRequest = JsonObject()

                jObjRequest.addProperty("assign_id",ClassModelInstance.getInstance().classInfo.assignId)
                jObjRequest.addProperty("type","2")
                val attendanceArray = JsonArray()
                for(i in 0 until ClassModelInstance.getInstance().studentInfos.size){
                    val jsonObject = JsonObject()
                    jsonObject.addProperty("stud_id",ClassModelInstance.getInstance().studentInfos[i].studId)
                    jsonObject.addProperty("attendanceID",1)
                    jsonObject.addProperty("stud_attendance",ClassModelInstance.getInstance().studentInfos[i].studAttendance)
                    attendanceArray.add(jsonObject)
                }

                jObjRequest.addProperty("attendance_list",attendanceArray.toString())
                Log.i("PritishAttendanceApi2", jObjRequest.toString())

                val submitAttendanceInterface = ApiClient.client.create(SubmitAttendanceInterface::class.java)

                submitAttendanceInterface.takeAttendance(jObjRequest)
                                .enqueue(object : Callback<SubmitAttendanceResponse> {
                    override fun onFailure(call: Call<SubmitAttendanceResponse>, t: Throwable) {
                        activity?.let { it1 -> ToastMaker.make(it1,getString(R.string.something_went_wrong),Toast.LENGTH_LONG) }
                        Log.i("Pritish",t.message+"\t"+t.localizedMessage+"\t"+t.printStackTrace()+"\t"+t.cause+"\n"+call.request())
                        alertDialog.dismiss()
                    }

                    override fun onResponse(call: Call<SubmitAttendanceResponse>, response: Response<SubmitAttendanceResponse>) {
                        if(response.body()?.status.toString().equals("1",true)){
                            activity?.let { it1 -> ToastMaker.make(it1,response.body()?.msg.toString(),Toast.LENGTH_LONG) }
                            goToPreviousFragment()
                        } else {
                            activity?.let { it1 -> ToastMaker.make(it1,response.body()?.msg.toString(),Toast.LENGTH_LONG) }
                        }

                        alertDialog.dismiss()
                    }

                })

这是接口和响应类

interface SubmitAttendanceInterface {

    @Headers("Content-Type: application/json")
    @POST("timetable/takeAttendance")
    fun takeAttendance(@Body body: JsonObject): Call<SubmitAttendanceResponse>

}

data class SubmitAttendanceResponse(
        @SerializedName("status")
        @Expose
        var status: Int? = null,
        @SerializedName("msg")
        @Expose
        var msg: String? = null

)

使用HttpInterceptor登录时,我得到com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path

我在堆栈溢出中搜索了以上错误,但答案不符合我的要求

JSON Error "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"

"Expected BEGIN_OBJECT but was STRING at line 1 column 1"

我不想公开URL,因此已在日志中编辑了URL。

1 个答案:

答案 0 :(得分:1)

根据您的日志和样本数据,您应该将数据“ attendance_list”作为插入字符串的json数组发布

尝试 jObjRequest.add("attendance_list",attendanceArray) 装的 jObjRequest.addProperty("attendance_list",attendanceArray.toString())