Loading and Interaction of a Map from Web Service

30 views
Skip to first unread message

rdvg...@gmail.com

unread,
Jul 25, 2019, 2:03:54 PM7/25/19
to CodenameOne Discussions
Hi,
There are simple things that still cost me and I need to solve a silly interaction problem of a Map.

My project extracts information from a web service using the new Rest API.
My problem is that the moment I try to interact the Map, all the information comes in a single register.
Remark: Menu is a class that implements PropertyBusinessObject.

My code:

        Response<Map> jMenu = Rest.get(SERVER_URL + "usuario/busquedaMenu").queryParam("token", Preferences.get("token", null)).acceptJson().getAsJsonMap();
        if (jMenu.getResponseCode() != 200) {
            Dialog.show("Error", "Lo sentimos, No existe un Menú definido para esta empresa", "Continuar", null);
            return;
        }
        Map<Integer, Menu> m = jMenu.getResponseData();

        for (Map.Entry<Integer, Menu> entry : m.entrySet()) {
            System.out.println("clave=" + entry.getKey() + ", valor=" + entry.getValue());
        }

Shai Almog

unread,
Jul 25, 2019, 10:52:30 PM7/25/19
to CodenameOne Discussions
Hi,
while the network request happens off the EDT the rest of this code is blocking. Try replacing getAsJsonMap with fetchAsJsonMap which would be a bit faster.

rdvg...@gmail.com

unread,
Jul 26, 2019, 8:56:21 AM7/26/19
to CodenameOne Discussions
Hi,

Thank you Mr. Shai, but how do I solve the problem that the information arrives in a single record? 

rdvg...@gmail.com

unread,
Jul 26, 2019, 8:58:52 AM7/26/19
to CodenameOne Discussions
Hi,

Variables.png

The figure shows the structure of the data returned by the web service and comes in a single record.

rdvg...@gmail.com

unread,
Jul 26, 2019, 10:16:38 AM7/26/19
to CodenameOne Discussions
Hi,

The following code shows how I solved the problem, I need to know if there is a simpler way to do it.

        Response<Map> jMenu = Rest.get(SERVER_URL + "usuario/busquedaMenu").queryParam("token", Preferences.get("token", null)).acceptJson().getAsJsonMap();
        if (jMenu.getResponseCode() != 200) {
            Dialog.show("Error", "Lo sentimos, No existe un Menú definido para esta empresa", "Continuar", null);
            return;
        }
        Map<Integer, Menu> m = jMenu.getResponseData();

        ArrayList tMenu = (ArrayList) Result.fromContent(m).getAsArray("root");
        ArrayList aMenu = new ArrayList();
        for (int i = 0; i < tMenu.size(); i++) {
            aMenu.add(populaRegistroMenu(tMenu.get(i)));
        }


    private Menu populaRegistroMenu(Object r) {
        Menu m = new Menu();
        m.getPropertyIndex().populateFromMap((Map) r);
        return m;
    }

Shai Almog

unread,
Jul 27, 2019, 12:56:31 AM7/27/19
to CodenameOne Discussions
Hi,
you can use fetchAsPropertyList() which automatically fills up the business objects for you.
Reply all
Reply to author
Forward
0 new messages