0

I'm trying to resolve the malformed JSON issue while dealing with the JSON object. It dumps the error on my project's console stating that Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $. How can I fix this?

Send Remote Message

private fun sendRemoteMessage(body: sender) {
    val retrofit = Retrofit.Builder()
        .baseUrl("http://fcm.googleapis.cm/fcm/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    retrofit.create(ApiService::class.java).sendRemoteMessage(body).enqueue( object :Callback<String> {

        override fun onFailure(call: Call<String>, t: Throwable) {
            Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
        }

        override fun onResponse(call: Call<String>, response: Response<String>) {
            if (response.code() == 200 && response.isSuccessful){
                Toast.makeText(applicationContext, "Message sent",Toast.LENGTH_LONG).show()
            }
            else {
                Toast.makeText(applicationContext, response.message(), Toast.LENGTH_LONG).show()
            }
        }
    })
}

API Service

@Headers({
       "Content-Type:application/json",
"Authorization:key=myServerKey"
    })
    @POST("send")
    Call<String> sendRemoteMessage(@Body sender body);

Handling Data

private fun initiateMeeting(meetingType: String, receiverToken: String) {
    val data = user()
    data.name = preferenceManager.getString(Constants().keyUserName).toString()
    data.profilePic = preferenceManager.getString((Constants().keyProfile)).toString()
    data.email = preferenceManager.getString(Constants().keyEmail).toString()
    data.token = preferenceManager.getString(Constants().keyRemoteMsgSenderToken).toString()

    val sender = sender(data, receiverToken)

    sendRemoteMessage(sender)
    Toast.makeText(applicationContext, sender.data.toString(), Toast.LENGTH_LONG).show()
}

Data Sender

class sender(var data: data, var to: String)

Data Class

class user: Serializable {
    var name = ""
    var profilePic = ""
    var token: String = ""
    var email = ""
}
Saurabh
  • 745
  • 1
  • 9
  • 33
  • It might be the headers you're sending. There should always be a space between the `:` colon and the content of the header. It might get wrongly interpreted as the request content otherwise. If it isn't that, also tell us where exactly this error happens including the stack trace. – JensV Aug 17 '20 at 09:55
  • But I put space after colon : still facing the same issue – Venkatesh Paithireddy Aug 24 '20 at 10:10
  • You still need to provide more info. Show us the stack trace, what are you parsing, do you have the response data, is it your own api? – JensV Aug 24 '20 at 10:53

0 Answers0