In my application on android, you need to send get requests using retrofit, this request should have, I think so, certain parameters, I was thrown by the backenders:

HTTP 200 { "count": int, # number of messages "messages": [ # array of messages { "id": int, # message id "subject": str, # message subject "can_delete": int, # can it be deleted (1) or not (0) "new": int # message already read (0) or not (1) "date": str, # date of message in 'dmy'. If message was sent today format will be 'H:M' "receiver_name": str, # name of receiver if type=1 "sender_name": str, # name of sender if type=0 }, ... ], "next_url": URL, # url for get next messages, if no more messages value is null "previous_url": URL # url for get previous messages, if no more messages value is null } 

and here I have a question - in double brackets we have the parameters and then their units of measure are written, and I cannot understand how to build a query with the above parameters, and then how to initialize this interface. I have an interface for building all requests from the application:

 Call<List<IncomeMessages>> getInMess(@Query("count") Integer method); 

if I understand correctly (as it was written in my sources) you need to use query in order to prescribe all variables. Here in a successful answer there is a certain ramification, for example, messages consists of separate qualifiers, and I need to create a new get request only for this field, or all variables, count, messages, next / previous url, can be placed within one request. What I still do not quite understand how I later distinguish the data after receiving them from the server. I would be very grateful for the help in solving this request, as I have been sitting all weekend.

    1 answer 1

    Most likely you didn’t share all the information that the backend developers provided you, but looking at what you showed, I would say that the above code gives an example of a successful server response to a request, and the data is returned in json format. In order to correctly process the entire response received, you need to create a class in which all the fields of this response (the so-called POJO class) will be described and it is the object of this class that should be requested to return Retrofit.

     import com.google.gson.annotations.SerializedName; public class Message { @SerializedName("id") int id; @SerializedName("subject") String subject; @SerializedName("can_delete") int isCanDelete; @SerializedName("new") int isNew; @SerializedName("date") String date; @SerializedName("receiver_name") String receiverName; @SerializedName("sender_name") String senderName; } import com.google.gson.annotations.SerializedName; import java.net.URL; import java.util.List; public class MessageAnswer { @SerializedName("count") int count; @SerializedName("messages") List<Message> messages; @SerializedName("next_url") URL nextUrl; @SerializedName("previous_url") URL previousUrl; } 

    And the Retrofit interface itself can then look like this:

     @Headers("Content-type: application/json") @GET("/list?type=0") Call<MessageAnswer> getInMess(@Query("count") Integer method); 
    • at the moment it is almost all the information, the rest is the answers in case of an unsuccessful answer. I think that these answers will help you nothing)) as far as I understand from your answer, you state that you need to create two classes - for the request and for the answer. Since now I use one class to send a request. - Andrew Goroshko 2:53 pm
    • Not really. In my answer, I recommend making a class for an object inside an array (message object class {"id": int, # message id "subject": str, # message subject ....) and the class of the answer itself, what the list of messages itself stores , number, next url, previous url. And from retrofit, it is the response class that needs to be parsed, in which there will already be a list of messages. It’s just that your question is still not completely understood. What exactly is the difficulty with you? - redlabrat
    • I have difficulty with writing parameters to the interface and further initializing this interface in the main class. I can't understand what to write or where.) - Andrew Goroshko
    • Your sample specifications did not specify query parameters. In this sample code, only the sample server response is indicated. - redlabrat
    • What is the query specification? something like post / get? - Andrew Goroshko