0

I have used retrofit2. but I cannot catch the response because there is always showing zero data. How can I receive a response? I am using this code. (added debug code also for better understanding).

ApiClient.getApiClient().getPurchOrderEdit(map).enqueue(new Callback<Map<String, Map<String, List<Map<String, Object>>>>>() {
            @Override
            public void onResponse(Call<Map<String, Map<String, List<Map<String, Object>>>>> call, Response<Map<String, Map<String, List<Map<String, Object>>>>> response) {
                System.out.println("test");
            }

            @Override
            public void onFailure(Call<Map<String, Map<String, List<Map<String, Object>>>>> call, Throwable t) {

            }
        });

enter image description here

{
    "TBLT_PURCHASE_ORDER_EDIT": {
        "data": [
            {
                "column_id": "17705",
                "po_id": "15872311254332020-04-18 23:32:05",
                "db_id": "62",
                "dbhouse_name": "Coca Cola DB House",
                "depot_id": "3",
                "dpo_name": "Tongi Depot",
                "dist_emp_id": "140",
                "first_name": "Coca cola DB",
                "order_date": "2020-04-18",
                "total_order": "1056",
                "total_quantity": "24"
            }
        ]
    }
}
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
paul polash
  • 1,108
  • 10
  • 10

1 Answers1

0

Make your API call to return ApiResult

Your ApiResult class:

public class ApiResult {

    private String status;
    private JsonElement result;

    public String getStatus() {
        return status;
    }

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

    public JsonElement getResult() {
        return result;
    }

    public void setResult(JsonElement result) {
        this.result = result;
    }

}

Use this code inside of Api onResponse and replace YourDataObject with your data class :

 Type typeToken = new TypeToken<List<YourDataObject>>() {
                            }.getType();

 List<YourDataObject> list = new ArrayList<>();

list = new Gson().fromJson(response.body().getResult().getAsJsonObject()
                                                .getAsJsonArray("data"), typeToken);

Krishna Sony
  • 1,286
  • 13
  • 27