Following is my request parameters in 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"}]}
Following is the response
{"status":1,"msg":"Success"}
Now in my Android App I am using Retrofit with Gson. But passing through Gson, I was facing some problem so I am sending request parameters in form of jsonObject
and jsonArrays
.
Following is my code when a button is pressed to submit request to server
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()
}
})
This is the interface and response class
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
)
When I log using HttpInterceptor
I get com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path
I searched Stack Overflow for the above error but the answers didn't met my requirement
"Expected BEGIN_OBJECT but was STRING at line 1 column 1"
I have edited the url in the logs as I don't want to expose the URL.
File: /home/diceapp/public_html/dize/application/libraries/REST_Controller.php
– Manthan Patel Aug 28 '18 at 12:56Line: 815
Function: set_status_header