通过翻新Android创建获取请求

时间:2018-07-24 07:29:10

标签: android client client-server retrofit retrofit2

我有一个应用程序,我必须在其中获取收入消息列表,该列表将被分类为Listview。我从服务器开发人员那里获得了GET请求的一些详细信息:

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 'd.m.y'. 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
}

现在,我创建了一个Interface

 @Headers("Content-type: application/json")
    @GET("/v1/message/list?type=TYPE")
    Call<List<IncomeMessages>> getInMess(@Query("count") String count,
                                         @Query("messages") String messages,
                                         @Query("id") Integer id,
                                         @Query("subject") String subject,
                                         @Query("new") Integer new_m,
                                         @Query("date") String date,
                                         @Query("receiver_name") String receiver_name,
                                         @Query("sender_name") String sender_name,
                                         @Query("next_url") URL next_url,
                                         @Query("previous_url") URL previous_url);

然后,我创建了一个简单的Class请求:

public class IncomeMessages
{
    @SerializedName("subject")
    private String subject;
    @SerializedName("date")
    private String date;
    @SerializedName("sender_name")
    private String sender_name;
    @SerializedName("receiver_name")
    private String receiver_name;

    public IncomeMessages(String sender_name, String date, String receiver_name, String subject) {
        this.sender_name = sender_name;
        this.date = date;
        this.receiver_name = receiver_name;
        this.subject = subject;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSender_name() {
        return sender_name;
    }

    public void setSender_name(String sender_name) {
        this.sender_name = sender_name;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getReceiver_name() {
        return receiver_name;
    }

    public void setReceiver_name(String receiver_name) {
        this.receiver_name = receiver_name;
    }
}

现在我还不知道如何从parameters获取所有这些server并将其分类为Listview。我还有一个request:curl -i -X GET -H "Content-Type:application/json" -H "Authorization:Bearer $ACCESS_TOKEN" https://server/v1/message/list?type=TYPE的样本,我是Retrofit请求的开始者,也许以前有人解决过类似的问题。因此,我认为您将帮助我解决以下问题。 (对不起,我的英语不好:)

1 个答案:

答案 0 :(得分:1)

这可能会帮助您如何在Ratrofit的 MainActivity 中编写代码。

  
    

Retrofit将在后台线程上下载并解析API数据,然后通过onResponse或onFailure方法将结果返回给UI线程。

  
ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<ResponceClass> call = apiService.getyourInterfacemethod(); /* Here Define Mathod Name as Same as Define in Interface and Pass here Your Interface @GET or @POST Method Parameter value */
    call.enqueue(new Callback<ResponceClass>() {
        @Override
        public void onResponse(Call<ResponceClass>call, Response<ResponceClass> response) {
            //Handle Api Response Here.

            List<String> list = response.body().getResults();   /* Make Your List or Something You Want to do As Your Requirement. */
            Log.d(TAG, "Number of List Record received: " + list.size());
        }

        @Override
        public void onFailure(Call<ResponceClass>call, Throwable t) {
            // Log error here since request failed
            Log.e(TAG, t.toString());
        }
    });