-1

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

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"

I have edited the url in the logs as I don't want to expose the URL.

BraveEvidence
  • 53
  • 11
  • 45
  • 119
  • add httplogginginterceptor to your retrofit client to get the log of network call – Manthan Patel Aug 28 '18 at 12:35
  • @Nudge Please check are you using correct url for the post request and posting correct json body to server. – Nainal Aug 28 '18 at 12:43
  • @ManthanPatel i am using that. Please read the question again i have mentioned it. If you want i can send you the logs – BraveEvidence Aug 28 '18 at 12:45
  • Yes send the log – Manthan Patel Aug 28 '18 at 12:46
  • @Nainal I am using the correct url. I checked it. – BraveEvidence Aug 28 '18 at 12:46
  • @ManthanPatel Please check the question . I have added the logs. I have edited the url in the logs as i dont want to expose the url – BraveEvidence Aug 28 '18 at 12:50
  • you are getting some more data

    File: /home/diceapp/public_html/dize/application/libraries/REST_Controller.php
    Line: 815
    Function: set_status_header

    – Manthan Patel Aug 28 '18 at 12:56
  • @ManthanPatel I dont think so i should be getting more data as Postman,Volley and ios gives the data i mentioned above in the question – BraveEvidence Aug 28 '18 at 12:58
  • try jObjRequest.addProperty("attendance_list",attendanceArray) insted of jObjRequest.addProperty("attendance_list",attendanceArray.toString()) in your sample data you are passing as an array and your logcat says it is string – Manthan Patel Aug 28 '18 at 13:00
  • @ManthanPatel if i remove toString() i am getting error at addProperty – BraveEvidence Aug 28 '18 at 13:01
  • sorry there was typo try this jObjRequest.add("attendance_list",attendanceArray) – Manthan Patel Aug 28 '18 at 13:02
  • @ManthanPatel its working. Please post it as an answer – BraveEvidence Aug 28 '18 at 13:07
  • Nudge, your posts are still too sloppy, as per my previous remarks. Please trim back the chatty material, and use an upper case letter when referring to yourself ("I"). These are not particularly onerous requests. Stack Overflow is a Q&A site where technical writing is appropriate, and it is not a chat-room. – halfer Aug 29 '18 at 11:12

1 Answers1

1

as per your log and sample data you should post data "attendance_list" as an json array insted of string

try jObjRequest.add("attendance_list",attendanceArray) insted of jObjRequest.addProperty("attendance_list",attendanceArray.toString())

Manthan Patel
  • 993
  • 1
  • 9
  • 20