如何通过使用翻新库在Android中显示列表?

时间:2018-11-23 06:39:37

标签: java android json gson retrofit

我必须在活动中显示一个列表

我的API密钥是:

http://api.cuidadotechnologies.com/NSSPL/leave_dtls.php

使用GSON转换器和改造库。 这个API会像这样在JSON中引发响应

{
  "status": 0,
  "response_data": [
    {
      "id": "12",
      "uid": "USER00000003",
      "reason": "Test",
      "type": "Plan Leave",
      "SataDate": "2018-09-18",
      "EndDate": "2018-09-25",
      "ApprovedBy": "USER00000002",
      "ApprovedDate": "2018-09-18",
      "Status": "REJECTED",
      "Remarks": "Test Reject"
    },
    {
      "id": "13",
      "uid": "USER00000003",
      "reason": "Wedding",
      "type": "Plan Leave",
      "SataDate": "2018-01-28",
      "EndDate": "2018-02-05",
      "ApprovedBy": "USER00000002",
      "ApprovedDate": "2018-09-18",
      "Status": "APPROVED",
      "Remarks": "Ok"
    }
  ]
}

我是这种方法的新手,请帮助我逐步进行此操作。

2 个答案:

答案 0 :(得分:0)

您可以使用use POJO类将JSON转换为类。使用下面的网站将JSON转换为POJO。

  

http://www.jsonschema2pojo.org

之后,您可以使用Retrofit来调用API并获得响应。从此站点获取参考:https://square.github.io/retrofit/

转换为类后,可以使用Gson方法进行转换。

SomeModelClass responseModel = new Gson().fromJson(response, SomeModelClass.class);

如果您不想手动执行此操作,则可以使用此addConverterFactory(GsonConverterFactory.create())改造方法将响应直接转换为类模型。

最后,您可以使用ViewHolder Pattern创建适配器,并使用RecyclerView使用该适配器。

答案 1 :(得分:0)

尝试这种方式。 制作改造对象。

public class ApiClient {
private final static String BASE_URL = "http://api.cuidadotechnologies.com/NSSPL/";
public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}



private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}

}

用于api调用的make接口。

public interface ApiInterface {
@GET("leave_dtls.php")
Call<ResponseData> getData();
}

使pojo类得到响应。

public class ResponseDataItem{

@SerializedName("Status")
private String status;

@SerializedName("uid")
private String uid;

@SerializedName("reason")
private String reason;

@SerializedName("ApprovedDate")
private String approvedDate;

@SerializedName("Remarks")
private String remarks;

@SerializedName("ApprovedBy")
private String approvedBy;

@SerializedName("id")
private String id;

@SerializedName("type")
private String type;

@SerializedName("EndDate")
private String endDate;

@SerializedName("SataDate")
private String sataDate;

public void setStatus(String status){
    this.status = status;
}

public String getStatus(){
    return status;
}

public void setUid(String uid){
    this.uid = uid;
}

public String getUid(){
    return uid;
}

public void setReason(String reason){
    this.reason = reason;
}

public String getReason(){
    return reason;
}

public void setApprovedDate(String approvedDate){
    this.approvedDate = approvedDate;
}

public String getApprovedDate(){
    return approvedDate;
}

public void setRemarks(String remarks){
    this.remarks = remarks;
}

public String getRemarks(){
    return remarks;
}

public void setApprovedBy(String approvedBy){
    this.approvedBy = approvedBy;
}

public String getApprovedBy(){
    return approvedBy;
}

public void setId(String id){
    this.id = id;
}

public String getId(){
    return id;
}

public void setType(String type){
    this.type = type;
}

public String getType(){
    return type;
}

public void setEndDate(String endDate){
    this.endDate = endDate;
}

public String getEndDate(){
    return endDate;
}

public void setSataDate(String sataDate){
    this.sataDate = sataDate;
}

public String getSataDate(){
    return sataDate;
}

}

最终回应。

public class ResponseData {

@SerializedName("response_data")
private List<ResponseDataItem> responseData;

@SerializedName("status")
private int status;

public void setResponseData(List<ResponseDataItem> responseData){
    this.responseData = responseData;
}

public List<ResponseDataItem> getResponseData(){
    return responseData;
}

public void setStatus(int status){
    this.status = status;
}

public int getStatus(){
    return status;
}

}

以这种方式将api称为片段或活动。

 ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<ResponseData> responseDataCall=apiInterface.getData();
    responseDataCall.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
            if (response.isSuccessful() && response.body()!=null && response!=null){
                List<ResponseDataItem> data=response.body().getResponseData();
            }
        }

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