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
Developer
Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Confused by threads

Hello fellow BB java developers. I've just started working on a new Blackberry 7 application for my job but I'm running into some difficulties understanding how to use threads and FieldChangeListeners properly.

 

For my first application, I'm trying to make a simple HTTP Post request and send along input a user enters into a text field. After pressing the submit button, the HTTP request will be sent and if the request was successful, I'll be updating another text field.

 

Here is the code I'm having trouble with:

 

public final class InputPage extends MainScreen
{
	
	
	private TextField inputTextField;
	private ButtonField submit;
	private TextField outputTextField;
	
	/**
     * Creates a new MyScreen object
     */
    public InputPage()
    {
    	
        // Set the displayed title of the screen       
        setTitle("Enter data below");
        

        inputTextField = new TextField();
        outputTextField = new TextField();
        submit = new ButtonField("Submit", ButtonField.CONSUME_CLICK);
        
        
        submit.setChangeListener(new FieldChangeListener(){

			public void fieldChanged(Field field, int context) {
				// Clear the textfield
				
				final String userInput = inputTextField.getText();
				inputTextField.clear(context);
				
				// Start a new thread and use it to make an HTTP connection.
				new Thread() {
					
					public void run() {
						myHTTP http = new myHTTP(myHTTP.MY_TEST_URL);
						
						// Send the user's input:
						if(http.fetch(userInput)) {
							
							UiApplication.getApplication().invokeLater(new Runnable() {
								public void run() {
									outputTextField.setText("Fetch worked");	
								}
								
							});							
						}
						else {
							UiApplication.getApplication().invokeLater(new Runnable() {
								public void run() {
									outputTextField.setText("Fetch failed");	
								}
								
							});
						}						
					}
					
				};
			}       	
        });
        
        
        add(inputTextField);
        add(submit);
        add(outputTextField);
        
    }    
}

 

 

The event listener for the button works for clearing the inputTextField, but the outputTextField never gets updated.

What am I misunderstanding here?

 

Any feedback is appreciated. Thanks.

Highlighted
Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Confused by threads

difficult to say with the code you posted as "myHTTP" seems to do the heavy lifting.

i can give you a few general tips though:
it is usually easier to read if you encapsulate all networking in their own classes/services.

usually asynchronous processing uses a callback mechanism to inform the UI that something has happened.
To do that you can create a simple interface, let your UI class implement that and hand it as an argument to your networking class.
This gives your code better structure and makes it easier to read and debug.

If something does not work, try to simplify your code until it works, then go step by step until it breaks again. This works especially well when you use sample applications as a base.
----------------------------------------------------------
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
Developer
Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Re: Confused by threads

Simon, Thank you for replying so quickly. The "myHTTP" class is essentially the example code taken from the HttpConnection Java API example.

 

I was just reading about the UiApplication class and noticed this one part in particular:

 

"Note that a UI application must follow rules similar to those of traditional Swing applications. In particular

*Only one thread at a time (generally, the event-dispatching thread) may gain access to an interface component at once.
*If you need access to the UI from outside event-handling or drawing code, then you can use the invokeLater() or invokeAndWait() methods."

 

Does that second point mean that I do not need to call UiApplication.getApplication().invokeLater(....) since I'm aleady in an event-handling procedure?

 

Also, I noticed that when I made the new Thread object, I never ran start() or call() on it. Is that required?

 

 

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Confused by threads

indeed, you have to call start() to launch your thread. i did not see that it was missing!

basically the whole application runs in the event thread.
if you start a new thread it is outside of this scope, you use invokeLater to get into the UI scope again and manipulate fields etc.
----------------------------------------------------------
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
Developer
Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Re: Confused by threads

After calling the start() method on my Thread object, I see a debug statement I put in there. The strange part however is that when debugging the stepper stops on the creation of the myHTTP object even though there is not breakpoint there. I have another debug statement inside the constructor of the myHTTP class but never see its output.

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Confused by threads

maybe an exception is thrown? the debugger usually stops at exceptions.

i would suggest that you take a sample, see if that works, the go from there.
----------------------------------------------------------
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
Developer
Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Re: Confused by threads

Simon,

 

You're right. There was an error but it didn't stand out right away. During the compilation and packaging process I noticed a class was throwing a "NoClassDefFoundError." After some searching, I found that it had to do with string concatenation of all things as discussed in the thread below:

 

http://www.blackberryforums.com/developer-forum/93868-java-lang-stringbuilder-warning-no-definition-...

 

After changing my java target from 1.5 to 1.4 (the difference being how string concatenation works: StringBuilder vs StringBuffer) and recompiling, everything worked correctly.

 

It looks like the issue was actually several issues compounded together and in turn that was making it very difficult for me to identify what was actually going wrong. Thanks for your help.