Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
Accepted Solution

How to refresh screen with http call automatically?

Hi there,

 

I'm trying to refresh my start screen every few seconds. On this screen I'm doing a http call to get data from a web service, further I've created a timer for refreshing. What should this timer do in its run-method? When I try to push the whole screen again nothing happens, when I put the try/catch-block with the http call in it I'm forced to declare the result object as final..

 

//refresh timer

TimerTask refreshTask = new TimerTask() {
	public void run() {
		UiApplication.getUiApplication().popScreen(getScreen()); 
		UiApplication.getUiApplication().pushScreen(new LoadingScreen());	
	}
};

new Timer().schedule(refreshTask, 5000);

// http call

try {
	http.call(soapAction, envelope);
	result = (SoapObject) envelope.getResponse();
}
catch { .. }

 

 

Might be obvious, but somehow I do not get this whole timer thing ;-)

 

Thanks in advance!! Musipoo

 

 

Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: How to refresh screen with http call automatically?

try using this in invoke and wait method
UiApplication.getUiApplication().popScreen(getScreen());
UiApplication.getUiApplication().pushScreen(new LoadingScreen());
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to refresh screen with http call automatically?

using invokeandwait will not help you there.

you should not execute blocking calls, such as http requests, on the event thread. see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

use a separate thread, see the httpdemo for a sample.

when the networking is finished you can use a callback (for example) to notify your UI of the change.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900

Re: How to refresh screen with http call automatically?

Okay, thank you so far! I've tried to restructure my program now so the http call is in a separate thread. I just have problems with the callback function.. I want to pass a String[][] and and int to my UI class, how can I do that with the callback object?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to refresh screen with http call automatically?

you could use the UI element as the callback class.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900

Re: How to refresh screen with http call automatically?

The whole UI depends on the amount of data I get, so there's not just one element that has to be changed.

I want to pass the String[][] and int and refresh the whole screen then - is that possible?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to refresh screen with http call automatically?

use the screen as a callback, then.

you pass a reference of the screen to the thread, when it is finished it calls a specified method of the screen. you can encapsulate this in an interface to make it cleaner.

don't forget to use invokelater within this function to get back on the event thread.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900

Re: How to refresh screen with http call automatically?

Sorry, I still do not quite understand and I can't find any similar examples.

Do you mean I should use the screen as the callback object? Something like this..?

 

MainScreen callbackObj;

public ConnectionThread(MainScreen mainScreen) {
	callbackObj = mainScreen;
}

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to refresh screen with http call automatically?

yes.
it would be cleaner to encapsulate it into an interface, like your screen implementing ConnectionCallback and your ConnectionThread taking a ConnectionCallback as a parameter. This would also allow other components to use the thread.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900

Re: How to refresh screen with http call automatically?

[ Edited ]

I tried to encapsulate as you wrote it. But when I want to call the event function from the new thread it gives me a NullPointerException..

 

public interface ConnectionCallback {
	public void processData();
}
// separate thread class with http call 
ConnectionCallback callbackObj; public ConnectionThread(ConnectionCallback conCallback) { callbackObj = conCallback;         // http call callbackObj.processData();         // at this point my program exits because of a NullPointerException }

 

public class MyScreen implements ConnectionCallback {

      public MyScreen() {

		ConnectionThread conThread = new ConnectionThread(myScreen);
		conThread.start();

                UiApplication.getUiApplication().invokeLater(new Runnable() {
	                public void run() {
                                // paint screen 
                                }
                });
      }

      public void processData(){
                  myArray = conThread.myArray;
	  resultLength = conThread.resultLength;
      }
}

 Did I get the idea wrong?