-3

I have to show A list in an Activity

MY API key is:

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

Using GSON converter and retrofit library. this API throws response in JSON like this

{
  "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"
    }
  ]
}

I am novice in this method please help me to do this step by step.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

You can use use POJO classes for converting JSON into classes. Use below website to convert JSON to POJO.

http://www.jsonschema2pojo.org

After that you can use Retrofit to call API and get response. Get reference from this site : https://square.github.io/retrofit/

After you convert into classes you can use Gson Methods for conversion.

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

You can use this addConverterFactory(GsonConverterFactory.create()) retrofit method to directly convert response into class model if you don't want to do it manually.

And finally you can Create adapter using ViewHolder Pattern and use that adapter with RecyclerView.

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

Try this way.. make retrofit object..

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;
}

}

make interface for api calling.

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

make pojo class for response.

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;
}

}

final response..

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;
}

}

called api into fragment or activity like this way..

 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();
        }
    });