Problems with a webservice and population in Json

45 views
Skip to first unread message

rdvg...@gmail.com

unread,
May 31, 2021, 12:14:58 PM5/31/21
to CodenameOne Discussions
Hi,

I am trying to consume a webservice using the restful API. It correctly returns the information I request:

data => [{usuarioid=17, usuario=demo1, nombreempresa=Embutidora Nacional, impuesto=7, menuid=1, dispositivoid=4, leehuella=1, imei=55555, correo=rube...@yahoo.com, nombre=Usuario Embutidos, radio=300, fuenteinventario=A, permitecambiarfoto=1, iniciosesion=1, contrasena=202cb962ac59075b964b07152d234b70, rangoinicio=100001, rangofin=150000, secuenciaorden=100054, estado=A, token=0a5b3d4a-5b8f-443d-af8e-cfb4100b71c4}]

To consume I do the following:

public static void login(Idioma idioma, String usuario, String contrasena, final SuccessCallback<Usuario> onSuccess, final FailureCallback<Object> onError) {  
        Rest.get(SERVER_URL + "blank_ws_autenticar/blank_ws_autenticar.php").
                acceptJson().
                queryParam("contrasena", contrasena).
                queryParam("usuario", usuario).
                getAsJsonMapAsync(new Callback<Response<Map>>() {

                    @Override
                    public void onSucess(Response<Map> value) {
                        usr = new Usuario();
                        PreferencesObject.create(usr).bind(); 
                        usr.getPropertyIndex().populateFromMap(value.getResponseData());
                        onSuccess.onSucess(usr);
                    }

                    @Override
                    public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
                        onError.onError(null, err, errorCode, errorMessage);
                    }
                });
    }

I cannot pass the information through the property "populateFromMap"
What is the correct way to do it?

Thanks

Shai Almog

unread,
May 31, 2021, 9:45:59 PM5/31/21
to CodenameOne Discussions
Hi,
It's hard to tell. Is the printout of data related to value.getResponseData()?
What are the fields of User?
How does it fail? Is the user object not set?
If you step over it with a debugger is it set for a short while?

rdvg...@gmail.com

unread,
May 31, 2021, 11:49:12 PM5/31/21
to CodenameOne Discussions
Hi,

In debug the value value.getResponseData () is:
Muestra.png
I have "usuario" class that implements PropertyBusinessObject to receive interaction "0" (see image).

When I try to receive the fields from the class they are in null (  usr.getPropertyIndex().populateFromMap(value.getResponseData());  ).

Thanks

Shai Almog

unread,
Jun 1, 2021, 9:45:43 PM6/1/21
to CodenameOne Discussions
Hi,
It's a bit hard to read in this way but it looks like the server returns a list of users.
So you would need something like this:
Map responseMap = value.getResponseData();
List entries = (List)responseMap.get("data");
usr.getPropertyIndex().populateFromMap((Map)entries.get(0));

rdvg...@gmail.com

unread,
Jun 1, 2021, 10:01:21 PM6/1/21
to CodenameOne Discussions
Hi,

Return error in List entries = (List)responseMap.get("data");

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.codename1.ui.List (java.util.ArrayList is in module java.base of loader 'bootstrap'; com.codename1.ui.List is in unnamed module of loader com.codename1.impl.javase.ClassPathLoader @5d5eef3d)
at com.innova507.server.UsuarioService$1.onSucess(UsuarioService.java:84)
at com.innova507.server.UsuarioService$1.onSucess(UsuarioService.java:68)
at com.codename1.io.rest.RequestBuilder$6.actionPerformed(RequestBuilder.java:582)
at com.codename1.io.rest.RequestBuilder$6.actionPerformed(RequestBuilder.java:567)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
at com.codename1.ui.Display.processSerialCalls(Display.java:1365)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1307)
at com.codename1.ui.Display.mainEDTLoop(Display.java:1189)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

rdvg...@gmail.com

unread,
Jun 1, 2021, 11:06:04 PM6/1/21
to CodenameOne Discussions
Hi,

In with a JSON validator on the internet I put the URL that I am using:

I was able to verify that the ws is working well as the validation resulted in:
[ { "usuario_id":"17", "usuario":"demo1", "nombre_empresa":"Embutidora Nacional", "impuesto":"7", "menu_id":"1", "dispositivo_id":"4", "lee_huella":"1", "imei":"55555", "numero_telefono":"64237770", "correo":"rube...@yahoo.com", "nombre":"Usuario Embutidos", "radio":"300", "fuente_inventario":"A", "permite_cambiar_foto":"1", "inicio_sesion":"1", "rango_inicio":"100001", "rango_fin":"150000", "secuencia_orden":"100054", "estado":"A", "token":"0a5b3d4a-5b8f-443d-af8e-cfb4100b71c4" }]

For some reason that I do not understand, I am resiving it as a list and therefore my problem must be with the client. The method to receive is:
 
public static void login(Idioma idioma, String usuario, String contrasena, final SuccessCallback<Usuario> onSuccess, final FailureCallback<Object> onError) {
          Rest.get(SERVER_URL + "blank_ws_autenticar/blank_ws_autenticar.php").
                acceptJson().
                queryParam("contrasena", contrasena).
                queryParam("usuario", usuario).
                getAsJsonMapAsync(new Callback<Response<Map>>() {

                    @Override
                    public void onSucess(Response<Map> value) {
                        usr = new Usuario();
                        PreferencesObject.create(usr).bind();
                        usr.getPropertyIndex().populateFromMap(value.getResponseData());

                        onSuccess.onSucess(usr);
                    }

                    @Override
                    public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
                        onError.onError(null, err, errorCode, errorMessage);
                    }
                });
      }

I appreciate you pointing out what I'm doing wrong.
Thanks

Shai Almog

unread,
Jun 2, 2021, 9:25:10 PM6/2/21
to CodenameOne Discussions
In JSON [] means array or list and {} means object or map. So you do have a list of maps in the jSON which is what you see in Java.

Sorry it wasn't clear but I means java.util.List not com.codename1.ui.List. If you fix your import the code I posted above should work.

rdvg...@gmail.com

unread,
Jun 3, 2021, 11:58:06 AM6/3/21
to CodenameOne Discussions
Hi,
Thank you very much, it works well.
Reply all
Reply to author
Forward
0 new messages