pop up error on Web service

21 views
Skip to first unread message

Amine

unread,
Jan 2, 2021, 5:39:18 AM1/2/21
to CodenameOne Discussions
Hi,
I have pop up showing exception even when catch it (try catch). How can i prevent this pop up??? I use rest.post.
Pleasz help,
Kind regards,
IMG-20210102-WA0025.jpg

Marisole Aromatherapy

unread,
Jan 2, 2021, 5:53:31 AM1/2/21
to codenameone...@googlegroups.com
Hi Amine,

There are a couple posts on this:


This way you can implement retries, etc

In my experience, you should have all bases covered if you implement

onError
onErrorCode(JSON/byte etc)

And then check (and retry or whatever you want to do without a popup if true)

resp.getResponseCode() != 200
resp.getResponseData() == null

If you know the expected result of a JSON response it's useful to also check it isn't null as sometimes network exceptions can return a non-null response but a null response body

Example: for a response that consists of {"result":"ok"}

You can check
((String) resp.getResponseData().get("result")) == null


A full JSON post implementation that retries automatically after waiting 1 second (and expects the above response format) would look like this

public Response<Map> safeSyncRestPost(String url, String body)
    {
        Boolean[] error = {false};
        RequestBuilder req = Rest.post(url)
                .onError(ee -> {
                    error[0] = true;
                }, true);
        if(body != null)
            req.body(body);
        Response<Map> resp = req.header("Content-Type", "application/json").onErrorCodeJSON(err -> {
            error[0] = true;
        })
        .getAsJsonMap();
        if (resp.getResponseCode() != 200) {
            ;//log error optionally
        } else if (error[0]) {
            ;//log error optionally
        } else if (resp.getResponseData() == null) {
            ;//log error optionally
        } else if (((String) resp.getResponseData().get("result")) == null) {
            ;//log error optionally
        } else {
                return resp;//success
        }
        try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {}
        return safeSyncRestPost(url, body);//retry
    }

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/fd58352c-8a25-465c-99db-89c04724a49co%40googlegroups.com.

Shai Almog

unread,
Jan 2, 2021, 9:26:23 PM1/2/21
to CodenameOne Discussions
The exception is caught and shown by that code. What you're seeing is a limitation of modern Android/iOS devices both of which require HTTPS traffic by default.
You can disable that but it might cause a problem moving forward in the app review process.

I would recommend adding a domain name and certificate to your server. There are free certificates you can use so this shouldn't be a problem.

Amine

unread,
Jan 6, 2021, 7:33:40 AM1/6/21
to CodenameOne Discussions
Hi,
It works thank for your help.
Kind regards
Amine
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages