Hello.

Please explain how Retrofit works. There is a page with json. In android connected Retrofit, okhttp, converter-gson. json this:

{"1": {"id":101, "item":"NameItem1", "itemId":"1", "name":"NameName", "img":"http:cccccc.jpg"}, "2": {"id":102, "item":"NameItem2", "itemId":"3", "name":"NameName", "img":"http:cccccc.jpg"}, "3": {"id":103, "item":"NameItem3", "itemId":"2", "name":"NameName", "img":"http:sddfsssds.jpg"}} 

1) In all examples, they write that you need to create a POJO class equivalent to JSON. I use http://www.jsonschema2pojo.org/ In my case it will be one MyClass. in which there will be variables Id, item, itemId, etc. with getters and settarmi. Or will it be several classes if you insert the whole json?

2) How does the get request in retrofit work?

 public interface APIService { // Ниже пишу название файла с json? @GET("list") // В Call передаю просто MyClass? Или список из MyClass? // В getData в аргументах перечисляю все переменные которые в MyClass? Call<List<MyClass>> getData(@Query("id") int id, @Query("item") String item ...); } 
  • This will not be a List. You have an object, not an array. This will be some kind of your class, which will have properties in which your MyClass objects will have to be written, and these properties will have @SerializedName ("1"), @SerializedName ("2") ... however you can write your own deserializer and turn this json into a list ... is your server? Maybe it's easier to fix it on the server ... - Yura Ivanov
  • @YuraIvanov. This means that option 2 in MyClass in which @SerializedName ("1"), @SerializedName ("2") will be written ... and in classes 1, 2, 3, the variables id, item, etc. will be already written. then Call <MyClass> will be? And what will getData come then? And as before id, item, etc. get there? With this approach, if you add 4 points, will you need to add a new class 4 and add @SerializedName ("4") to MyClass manually? The server is not mine. - Anatoly Ferisov

1 answer 1

It will be two classes

MyClassPack:

 public class MyClassPack { @SerializedName("1") @Expose public MyClass _1; @SerializedName("2") @Expose public MyClass _2; @SerializedName("3") @Expose public MyClass _3; } 

MyClass:

 public class MyClass { @SerializedName("id") @Expose public int id; @SerializedName("item") @Expose public String item; @SerializedName("itemId") @Expose public String itemId; @SerializedName("name") @Expose public String name; @SerializedName("img") @Expose public String img; } 

and change the resulting class, in the parameters you specify the necessary parameters for the request to the server, I can not answer exactly, because there is no information on how you get the data, for example: I get the list from the URL - http://some.com/list?id=5

 public interface APIService { @GET("list") Call<MyClassPack> getData(@Query("id") int id); } 

If you want one class, not two - an array should come

  • To get an array, you need to remove 1, 2, 3, in which id, item, etc. are nested? - Anatoly Ferisov
  • one
    to come in an array you need this jason: [{"id": 101, "item": "NameItem1", "itemId": "1", "name": "NameName", "img": "http: cccccc.jpg" }, {"id": 102, "item": "NameItem2", "itemId": "3", "name": "NameName", "img": "http: cccccc.jpg"}, {"id" : 103, "item": "NameItem3", "itemId": "2", "name": "NameName", "img": "http: sddfsssds.jpg"}] - Nikolai Konorev