简体   繁体   English

android:如何使用 Retrofit 接收来自以下 json 的响应?

[英]android: How to receive response from the following json using Retrofit?

I have used retrofit2.我用过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) {

            }
        });

在此处输入图像描述

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

Make your API call to return ApiResult拨打API电话返回ApiResult

Your ApiResult class:你的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:在 Api onResponse中使用此代码,并将YourDataObject替换为您的数据 class:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM