How to get array of objects with gson/retrofit?

后端 未结 1 1047
名媛妹妹
名媛妹妹 2021-01-12 14:11

I have used gson before to automatically convert to pojo\'s.

but now im trying to use retrofit to convert an api result to objects.

As long as the json has n

相关标签:
1条回答
  • 2021-01-12 14:56

    To Handle json response like this:

    [
        {"name":"foo"},
        {"name":"bar"}
    ]
    

    Use Call< List < AnItem>>


    In Retrofit2, HttpUrl.resolve() is used to resolve baseUrl and annotated path.

    Due to how this works,

    This works:

    .baseUrl("http://someurl.com")
    
    @GET("/api/itemlist")    --> resolved to http://someurl.com/api/itemlist
    

    or this works

    .baseUrl("http://someurl.com/api/")
    
    @GET("itemlist")    --->  resolved to http://someurl.com/api/itemlist
    

    However, this would not work

    .baseUrl("http://someurl.com/api")
    
     @GET("/itemlist")   ---> resolved to http://someurl.com/itemlist
    
    0 讨论(0)
提交回复
热议问题