如何从Android中的服务器请求获取一些数据

时间:2017-10-06 14:36:21

标签: android retrofit2

在我的应用程序中,我从服务器获取了一些数据,我想知道有多少数据是 Seen = true
在我的应用程序中,我从服务器获取了一些数据,我想知道有多少数据 Seen = true
要连接到服务器,请使用 Retrofit 2 我的Json:

{
  "statusCode": 200,
  "statusMessage": "",
  "data": [
  }
      "seen": true,
      "replierCount": 0,
      "additionalInfo": {}
    },{
      "seen": false,
      "replierCount": 0,
      "additionalInfo": {}
    },{
      "seen": false,
      "replierCount": 0,
      "additionalInfo": {}
    },{
      "seen": true,
      "replierCount": 0,
      "additionalInfo": {}
    },{
      "seen": true,
      "replierCount": 0,
      "additionalInfo": {}
    },{
      "seen": false,
      "replierCount": 0,
      "additionalInfo": {}
    }
  ]
}

我在活动中的代码:

ExploreSendData sendData = new ExploreSendData();
sendData.setPageIndex(1);
sendData.setPageSize(10);
sendData.setID(contentID);

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<ExploreResponse> call = api.getExplore(token, sendData);

call.enqueue(new Callback<ExploreResponse>() {
    @Override
    public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
        if (response.body().getData() != null) {
            if (response.body().getData().size() > 0) {

            }

    }

    @Override
    public void onFailure(Call<ExploreResponse> call, Throwable t) {
    }
});

我的回复模型:

public class ExploreResponse {

    @SerializedName("statusCode")
    @Expose
    private Integer statusCode;
    @SerializedName("statusMessage")
    @Expose
    private String statusMessage;
    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

}

我的基准模型:

public class Datum {

    @SerializedName("seen")
    @Expose
    private Boolean seen;
    @SerializedName("replierCount")
    @Expose
    private Integer replierCount;
    @SerializedName("additionalInfo")
    @Expose
    private AdditionalInfo additionalInfo;

    public Boolean getSeen() {
        return seen;
    }

    public void setSeen(Boolean seen) {
        this.seen = seen;
    }

    public Integer getReplierCount() {
        return replierCount;
    }

    public void setReplierCount(Integer replierCount) {
        this.replierCount = replierCount;
    }

    public AdditionalInfo getAdditionalInfo() {
        return additionalInfo;
    }

    public void setAdditionalInfo(AdditionalInfo additionalInfo) {
        this.additionalInfo = additionalInfo;
    }

}

我怎么知道有多少数据看到=真

请帮帮我。谢谢所有&lt; 3

2 个答案:

答案 0 :(得分:0)

请将此方法添加到ExploreResponse类,因为您必须手动计算Seen = true

public int getDataSeenAsTrueCount()
{
    int count = 0;
    for(Datum d in getData())
    {
        if(d.getSeen)
        {
            count++;
        }
    }

    return count;
}

答案 1 :(得分:0)

试试这段代码:

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<ExploreResponse> call = api.getExplore(token, sendData);

call.enqueue(new Callback<ExploreResponse>() {
    @Override
    public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
        if (response.body().getData() != null && response.body().getStatusCode() != 401
                && response.body().getStatusCode() != 402) {
            List<Datum> seen = new ArrayList<>();
            if (response.body().getData().size() > 0) {
                for (Datum datum : response.body().getData()) {
                    if (datum.getSeen()) {
                        seen.add(datum);
                    }
                }
                if (seen.size() > 0) {
                }
            }
        } else {
            prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
            startActivity(new Intent(context, LoginActivity.class));
        }

    }

    @Override
    public void onFailure(Call<ExploreResponse> call, Throwable t) {
    }
});