android – java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $ while UPDATE

Hello i am creating an android App for Students to easily find rooms to stay, as a School Project, and i am having some trouble with Update. I created an webservice with I already know put and Delete are paid, but my teacher told us we can embedded it in a POST and make it work.

In that way i was already able to put my DELETE to work. However i am having trouble with the UPDATE. when i try to update, all the data change to what i want to change it, however at the same time my OnFailure method is called and it gives me this error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $

Iam find it strange, because in my actiy i am using similar code to my POST (that works perfectly) and despite the error, my data is updated, however i want to get rid of the error, since it doesn’t allow me to change activity as intended.

Here is my webService PUT embedded in a POST (used slim 4 and NOTORM for the webservice)

//Put anuncios

$app->post('/api/editar_anuncios/{id}', function( $request, $response){
   $id = $request->getAttribute('id');  
   $users_id = $request->getParsedBody()["users_id"];
   $morada = $request->getParsedBody()["morada"];
   $n_quartos = $request->getParsedBody()["n_quartos"];
   $latitude = $request->getParsedBody()["latitude"];
   $longitude = $request->getParsedBody()["longitude"];
   $fotografia = $request->getParsedBody()["fotografia"];
   $preco = $request->getParsedBody()["preco"];
   $ncasas_banho = $request->getParsedBody()["ncasas_banho"];
   $telemovel = $request->getParsedBody()["telemovel"];
   $mobilado= $request->getParsedBody()["mobilado"];
   $outros_atributos = $request->getParsedBody()["outros_atributos"];
   $qrcode = $request->getParsedBody()["qrcode"];

   $data = array(
       "users_id"=> $users_id,
       "morada" => $morada,
       "n_quartos" => $n_quartos,
       "latitude" => $latitude,
       "longitude" => $longitude,
       "fotografia"=> $fotografia,
       "preco" => $preco,
       "ncasas_banho" => $ncasas_banho,
       "telemovel" => $telemovel,
       "mobilado" => $mobilado,
       "outros_atributos" => $outros_atributos,
       "qrcode" => $qrcode

    $result = $db->anuncios[$id]->update($data);

       echo json_encode('O anuncio foi atualizado com sucesso', JSON_UNESCAPED_UNICODE);
       return $response;
    } else
    echo json_encode('A atualização falhou', JSON_UNESCAPED_UNICODE);
    return $response;
   } else
   echo json_encode('O anuncio não existe', JSON_UNESCAPED_UNICODE);
   return $response;


my Endpoints

    fun editar(@Path("id") id: Int?,
               @Field("users_id") users_id: Int?,
                 @Field("morada") morada: String?,
                 @Field("n_quartos") n_quartos: Int?,
                 @Field("latitude") latitude: Double?,
                 @Field("longitude") longitude: Double?,
                 @Field("fotografia") fotografia: String?,
                 @Field("preco") preco: Double?,
                 @Field("ncasas_banho") ncasas_banho: Int?,
                 @Field("telemovel") telemovel: String?,
                 @Field("mobilado") mobilado: String?,
                 @Field("outros_atributos") outros_atributos: String?,
                 @Field("qrcode") qrcode: String?): Call<OutputEditar>

My ServiceBuilder

object ServiceBuilder {
    private val client = OkHttpClient.Builder().build()


    private val retrofit = Retrofit.Builder()

    fun<T> buildService(service: Class<T>): T {
        return retrofit.create(service)

My OutputEditar

data class OutputEditar(

    val users_id: Int,
    val morada: String,
    val n_quartos: Int,
    val latitude: Double,
    val longitude: Double,
    val fotografia: String,
    val preco: Double,
    val ncasas_banho: Int,
    val telemovel: String,
    val mobilado: String,
    val outros_atributos: String,
    val qrcode: String,
    val status: String,
    val MSG: String

The most important code of my Activity for Update. On the OnCreate i call the webservice to put the intended data on my views, and it work perfectly. Just put in here, to show how i get the variables to use on the method for my update

private lateinit var editMoradaView: EditText
    private lateinit var editNQuartosView: EditText
    private lateinit var shared_preferences: SharedPreferences
    private lateinit var latitude : EditText
    private lateinit var longitude : EditText
    private lateinit var imageView: ImageView
    private lateinit var button: Button
    private val pickImage = 100
    private var imageUri: Uri? = null
    private var base64:String? =null
    private var fotografia:String? =null
    private lateinit var decodedByte: Bitmap
    private var isBitMap:Boolean= false
    private var ID:Int = 0

override fun onCreate(savedInstanceState: Bundle?) {


        imageView = findViewById(
        button = findViewById(
        editMoradaView = findViewById(
        editNQuartosView = findViewById(
        latitude = findViewById(
        longitude = findViewById(
        button.setOnClickListener {
            val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
            startActivityForResult(gallery, pickImage)


        shared_preferences = getSharedPreferences("shared_preferences", Context.MODE_PRIVATE)
        val isLogin = shared_preferences.getBoolean("login",false )


        var id = intent.getStringExtra(DetalhesActivity.PARAM_ID)
        val request = ServiceBuilder.buildService(
        val call : Call<List<Anuncio>> = request.getAnunciosById2(id)

        call.enqueue(object : Callback<List<Anuncio>> {
            override fun onResponse(call: Call<List<Anuncio>>, response: Response<List<Anuncio>>) {
                if (response.isSuccessful) {
                    anuncios = response.body()!!
                    for (anuncio in anuncios) {

                        ID =

                        val decodedString: ByteArray = Base64.decode(anuncio.fotografia, Base64.DEFAULT)
                         decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)

                        isBitMap= true


            override fun onFailure(call: Call<List<Anuncio>>, t: Throwable) {
                Toast.makeText(this@EditarAnuncio,"${t.message}", Toast.LENGTH_LONG).show()


My Update method where i check if the file is a Bitmap or not, because when i choose a picture from my phone i change it to Bitmap and then to base64 string, where i insert it this way on my database . But when i get the picture from the database to show it on my ImageView, i change it from base64 to only bitmap and show it in that format on the screen . So i have to check which one of the format my file is, to update it properly (it is working as i intended , as the picture updates if i try to change it). I tried changing the default values ​​to test if everything updates, and everything updates as it should. However it still passes the error from the onFailure method and i don’t understand why it is happening, and what i can do to make the error go away. Thank You

fun editar(view: View) {

        val request = ServiceBuilder.buildService(
        val latitude = latitude.text.toString().toDouble()
        val longitude = longitude.text.toString().toDouble()
        val morada= editMoradaView.text.toString()
        val n_quartos = editNQuartosView.text.toString().toInt()
        val utilizador_id = shared_preferences.getInt("id", 0)

            val base = getBase64String(decodedByte)
             fotografia = base
            fotografia =  base64

        val call = request.editar(
            id = ID,
            users_id = utilizador_id,
            morada = morada,
            n_quartos = n_quartos,
            latitude = latitude.toString().toDouble(),
            longitude = longitude.toString().toDouble(),
            fotografia = fotografia,
            preco = 21.0,
            ncasas_banho = 4,
            telemovel= "teste3",
            mobilado = "teste3",
            outros_atributos = "2",
            qrcode = "teste3")

        call.enqueue(object : Callback<OutputEditar> {
            override fun onResponse(call: Call<OutputEditar>, response: Response<OutputEditar>){
                if (response.isSuccessful){
                    val c: OutputEditar = response.body()!!
                    Toast.makeText(this@EditarAnuncio, c.MSG, Toast.LENGTH_LONG).show()
                    val intent = Intent(this@EditarAnuncio,
            override fun onFailure(call: Call<OutputEditar>, t: Throwable){
                Toast.makeText(this@EditarAnuncio,"${t.message}", Toast.LENGTH_SHORT).show()



I tried testing my update in Postman by puting the Json like this, and it worked in Postman. However in my App it is still not working. and i don’t understand i am in my app is not getting in this format (it seems this way)

But when i try to insert data in my data base (with my code similar with the update) it works without any error. In my Update it works but not 100% , as it still gives me that error

"users_id": 1,
"n_quartos": 2,
"latitude": 20.0,
"longitude": 2.0,
"morada": "teste",
"fotografia": "teste",
"preco": 240,
 "ncasas_banho": 4,
"telemovel": 9242424242,
"mobilado": "teste",
"outros_atributos": "teste",
"qrcode": "teste"

Leave a Comment