BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Initialize the main screen and a web service call. (http://www.blackberryforums.com/developer-forum/161193-initialize-main-screen-web-service-call.html)

sickned 11-24-2008 06:42 AM

Initialize the main screen and a web service call.
 
Hi,

I have to do the next in my application:

- At the start, do a web service call, that returns some data. If the connection fails, I have to show a message at the screen.

I've tried everything, but the enterEventDispatcher method (it paints all) doesnt return.

How can I do it? Where can I call the web service call, once the screen is displayed?

Thanks in advance.

Ivanov 11-24-2008 07:04 AM

enterEventDispatcher never returns, otherwise your application will be closed.

Your WS call has to be done in a separate Thread, which can be either created and started in the screen's constructor or in your UiApplication's constructor before or after pushing your screen

sickned 11-24-2008 08:20 AM

Quote:

Originally Posted by Ivanov (Post 1180677)
enterEventDispatcher never returns, otherwise your application will be closed.

Your WS call has to be done in a separate Thread, which can be either created and started in the screen's constructor or in your UiApplication's constructor before or after pushing your screen

Thanks Ivanov. Can you post some sample code, please?

Another issue is the data from the WS has to be disponible for the UIApplication. How connect two threads?

Thanks in advance.

sickned 11-24-2008 08:52 AM

And another question. If I do the WS call in one thread and I get an exception, how can I send this to my app?

Thanks.

Ivanov 11-24-2008 08:53 AM

for WS call examples look here in forum for kSOAP2 if you're using it to call your WS.
For creating Threads refer to RIM's API javadoc

I prefer to use callbacks to inform a listener about new information. Search for a forum thread conserning it

Ivanov 11-24-2008 08:56 AM

Quote:

Originally Posted by sickned (Post 1180787)
And another question. If I do the WS call in one thread and I get an exception, how can I send this to my app?

Thanks.

If you only want to show a popup message you can use global screens. Otherwise use callbacks as mentioned above

sickned 11-24-2008 09:38 AM

Thanks Ivanov.

I've tried to use callbacks, but when I call the new Thread (who calls WS), the principal thread stay suspended or something.

I'll show you some code:

Code:

public class InicioThread extends Thread{
       
        PretiumController controller;
        InicioScreen screen;
       
        public InicioThread(PretiumController controller, InicioScreen screen){
                this.controller = controller;
                this.screen= screen;
        }
       
        public void run() {
                try {
                        sleep(1000);                       
                        controller.inicializarApp(); //THIS CALLS THE WS
                        screen.inicializarAplicación();
                } catch (Exception e) {
                        screen.informarExcepción(e.getMessage());
                }
               
        }
       
}


public class InicioScreen extends MainScreen {
                       
        PretiumController  controller;
       
       
        public InicioScreen(PretiumController controller){
                this.controller = controller;
               
                InicioLabelField inicio = new InicioLabelField(controller, "Iniciando aplicación...");
               
                add(inicio);       
               
               
               

        }
       
       
       
        public  void inicializarAplicación(){
               
                        try{                       
                                UiApplication.getUiApplication().pushScreen(new SeleccionClienteScreen(controller));
                               
                        }catch(Exception e){
                                Dialog.inform(e.getMessage());
                        }
                       
               
        }
       
        public void lanzarThread(){
                InicioThread t = new InicioThread(controller, this);
                t.run();
        }
       
        public void informarExcepción(String mensaje){
                Dialog.inform(mensaje);
        }


}

public class PretiumMain extends UiApplication{

        public static void main(String[] args) {
                PretiumMain theApp = new PretiumMain();               
                theApp.enterEventDispatcher();       
               
        }

        public PretiumMain() {

                        PretiumController controller = new PretiumController();
                       
                        InicioScreen inicio = new InicioScreen(controller);
                       
                        UiApplication.getUiApplication().pushScreen(inicio);
                       
                        inicio.lanzarThread();
                       
                       
       
        }
}

What Im doing wrong?

Ivanov 11-24-2008 09:52 AM

Code:

                InicioThread t = new InicioThread(controller, this);
                t.run();

Threads are starting when calling start() otherwise run() is executed in your current class content.

sickned 11-24-2008 10:06 AM

I've modified the code:

Code:

public class PretiumMain extends UiApplication{
       

       
        public static void main(String[] args) {
                PretiumMain theApp = new PretiumMain();                       
                theApp.enterEventDispatcher();       
               
        }

        public PretiumMain() {

                        PretiumController controller = new PretiumController();
                       
                        InicioScreen inicio = new InicioScreen(controller);

                        UiApplication.getUiApplication().pushScreen(inicio);

       
        }

}

public class InicioThread extends Thread{
       
        PretiumController controller;
        InicioScreen pantalla;
       
        public InicioThread(PretiumController controller, InicioScreen pantalla){
                this.controller = controller;
                this.pantalla = pantalla;
                this.setPriority(1);
        }
       
        public void run() {
                try {
                        sleep(2000);                       
                        controller.inicializarApp();
                        pantalla.inicializarAplicación();
                } catch (Exception e) {
                        pantalla.informarExcepción(e.getMessage());
                }
               
        }
       
}

public class InicioScreen extends MainScreen {
                       
        PretiumController  controller;
       
       
        public InicioScreen(PretiumController controller){
                this.controller = controller;
               
                InicioLabelField inicio = new InicioLabelField(controller, "Iniciando aplicación...");
               
                add(inicio);       
               
                InicioThread t = new InicioThread(controller, this);
               
                t.start();
               
               
               

        }
       
       
       
        public  void inicializarAplicación(){
               
                        try{                       
                                UiApplication.getUiApplication().pushScreen(new SeleccionClienteScreen(controller));
                               
                        }catch(Exception e){
                                Dialog.inform(e.getMessage());
                        }
                       
               
        }
       

       
        public void informarExcepción(String mensaje){
                Dialog.inform(mensaje);
        }
       
       

}

This doesnt work too. What I want is the InicioScreen be painted at the begining, while the InicioThread calls the webservice, so the user can see "Application loading...".

Thanks.

Ivanov 11-24-2008 10:15 AM

in your extended UiApplication constructor:

- create a screen
- create a ws thread passing a reference of your screen to it.
- push your screen
- start your thread

your thread should call a function when it gets the result, this function updates the screen fields. Take care about event lock

sickned 11-24-2008 10:28 AM

Quote:

Originally Posted by Ivanov (Post 1180936)
in your extended UiApplication constructor:

- create a screen
- create a ws thread passing a reference of your screen to it.
- push your screen
- start your thread

your thread should call a function when it gets the result, this function updates the screen fields. Take care about event lock

Thanks you very much but this doesnt work.


Code:

public class PretiumMain extends UiApplication{
       

       
        public static void main(String[] args) {
                PretiumMain theApp = new PretiumMain();                       
                theApp.enterEventDispatcher();       
               
        }

        public PretiumMain() {

                        PretiumController controller = new PretiumController();

                        InicioScreen inicio = new InicioScreen(controller);
                       
                        InicioThread t = new InicioThread(controller, inicio);

                        UiApplication.getUiApplication().pushScreen(inicio);
                       
                        t.start();

       
        }

}


sickned 11-24-2008 10:49 AM

Any help, please?

Ivanov 11-25-2008 03:06 AM

what exactly does not work?
If you haven't changed your screen's constructor you have 2 WS calling threads now.


All times are GMT -5. The time now is 04:29 AM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.