Home > Net >  «java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 2 column 1 path $» Kot
«java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 2 column 1 path $» Kot

Time:04-09

Hello i am trying to invoke a web service (using Retrofit) made by myself, with Slim4, however it is giving me the same error. I already went back and forth and tried many solutions i found on other threads however none of it worked yet .

This is my endpoint to do a get with Slim 4 and NOTORM library

$app->get('/api/anuncios', function ($request, $response) {
    require_once('db/dbconnection.php');
    foreach($db->anuncios()
    
            as $row){
                $data[] = $row;
            }
            echo json_encode($data, JSON_UNESCAPED_UNICODE);
            return $response;
    
});

These are my Endpoints on Android Studio (i am only trying, for now, the first one)

interface EndPoints {



    @GET("/RoomForStudents/api/anuncios/")
    fun getAnuncios(): Call<List<Anuncio>>

   /** @GET("/api/anuncios/{id}")
    fun getAnunciosById(@Path("id") id: Int): Call<Anuncio>*/
}

My ServiceBuilder (i already added the gson, to solve another error , about JSON malforming)

object ServiceBuilder {

   private val gson = GsonBuilder().setLenient().create()


    private val client = OkHttpClient.Builder().build()

    private val retrofit = Retrofit.Builder()
        /**.baseUrl("https://tneveda.000webhostapp.com/")*/
        .baseUrl("https://tneveda.000webhostapp.com/")
        .addConverterFactory(GsonConverterFactory.create(gson))
        .client(client)
        .build()

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

I think the problem is somehow with the format of the Data that is readed in my webService, because i made a test with this Dummy Data https://jsonplaceholder.typicode.com/users and it worked, however with the data of my WebService it doesn't work, however i am having troubling in finding a solution

Thank you

Just to add the result of the get endpoint of my webservice https://tneveda.000webhostapp.com/RoomForStudents/api/anuncios

EDIT

Noticed on postman, that after using my endpoints, i was getting the responde in html, and added this is the PHP file with the endpoints with SLIM/NOTORM ''' header("Content-Type: application/json; charset=UTF-8"); ''' Also changed JSON_UNESCAPED_UNICODE to JSON_PRETTY_PRINT

and now my response in the server is in JSON format and pretty

However it is still giving me the same error

CodePudding user response:

You should remove the last forward slash / from your @GET annotation.

@GET("/RoomForStudents/api/anuncios")
fun getAnuncios(): Call<List<Anuncio>>

As the last / is leading to a different endpoint, and your server is throwing a HttpNotFoundException.

  • Related