BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-24-2008, 06:42 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Initialize the main screen and a web service call.

Please Login to Remove!

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.

Last edited by sickned : 11-24-2008 at 06:43 AM.
Offline  
Old 11-24-2008, 07:04 AM   #2 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 11-24-2008, 08:20 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Ivanov View Post
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.
Offline  
Old 11-24-2008, 08:52 AM   #4 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Offline  
Old 11-24-2008, 08:53 AM   #5 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 11-24-2008, 08:56 AM   #6 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by sickned View Post
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
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 11-24-2008, 09:38 AM   #7 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

Last edited by sickned : 11-24-2008 at 09:39 AM.
Offline  
Old 11-24-2008, 09:52 AM   #8 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
		InicioThread t = new InicioThread(controller, this);
		t.run();
Threads are starting when calling start() otherwise run() is executed in your current class content.
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 11-24-2008, 10:06 AM   #9 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Offline  
Old 11-24-2008, 10:15 AM   #10 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 11-24-2008, 10:28 AM   #11 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Ivanov View Post
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();

	
	}

}
Offline  
Old 11-24-2008, 10:49 AM   #12 (permalink)
Knows Where the Search Button Is
 
Join Date: Oct 2008
Model: 7100T
PIN: N/A
Carrier: Unknow
Posts: 42
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any help, please?
Offline  
Old 11-25-2008, 03:06 AM   #13 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

what exactly does not work?
If you haven't changed your screen's constructor you have 2 WS calling threads now.
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2014 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.