«java.lang.IllegalStateException: 预期为 BEGIN_ARRAY,但在第 2 行第 1 列路径 $» 处为 STRING科特林错误

发布于 2025-01-20 06:27:37 字数 2036 浏览 3 评论 0原文

您好,我正在尝试使用 Slim4 调用我自己制作的 Web 服务(使用 Retrofit),但是它给了我同样的错误。我已经来回尝试了在其他线程上找到的许多解决方案,但还没有一个起作用。

这是我使用 Slim 4 和 NOTORM 库进行获取的端点

$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;
    
});

这些是我在 Android Studio 上的端点(我现在只是尝试第一个)

interface EndPoints {



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

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

我的 ServiceBuilder(我已经添加了 gson,以解决另一个关于 JSON 的错误)格式错误)

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)
    }
}

我认为问题出在我的 webService 中读取的数据格式,因为我用这个虚拟数据进行了测试 https://jsonplaceholder.typicode.com/users 并且它有效,但是对于我的 WebService 的数据它不起作用,但是我在寻找解决方案时遇到了麻烦

谢谢

只是添加我的网络服务的获取端点的结果 https://tneveda.000webhostapp.com/RoomForStudents/api/anuncios

编辑

注意到邮递员,在使用我的端点后,我得到了 html 格式的响应,并添加了这是带有端点的 PHP 文件修身/不规范 ''' header("内容类型:application/json; charset=UTF-8"); ''' 还将 JSON_UNESCAPED_UNICODE 更改为 JSON_PRETTY_PRINT

,现在我在服务器中的响应是 JSON 格式并且很漂亮

但是它仍然给我相同的错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2025-01-27 06:27:37

您应该从@get注释中删除最后一个前向斜杠/

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

由于最后一个/导致了不同的端点,并且您的服务器正在抛出httpnotfoundexception

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文