HttpConnection using Thread Behavior

Hello there Fellow Programmers!

May I ask a question?

I have noticed that whenever you implement anHttpConnection in J2ME it's required for it to use its own Thread, right?

I am using the commandAction(Command command, Displayable displayable)

event handler

to execute an event [okCommand] that invokes a Class which creates and opens an Http Connection.

The problem is that the statements that come After the Thread Calling execute Before the Thread Finishes Execution.How Can I prevent That from happening?

publicvoid commandAction(Command command, Displayable displayable){

// Insert global pre-action code here

if (displayable == formEnter){

if (command == commandOK1){

// Insert pre-action code here

PersonEntity person =new PersonEntity();

person.setId(Integer.parseInt(IdTextField.getString()));

person.start();//initiates run()

getDisplay().setCurrent(get_formWelcome());

// Insert post-action code here

System.out.print("I'm Executing before Thread finishes");

/*line-->*/ FrmWelStringItem.setText(person.getName);/*This line should execute After Thread finishes but doesn't*/

}

Could anyone please tell me How can I fix This?

I certainly appreciate all the Help, you guys are the best !

Sincerely, J

[1864 byte] By [BCE_Josea] at [2007-11-27 10:38:44]
# 1

Hi!.

Let's...to begin an http connection you are right, you must do it in another thread apart. That is cuz, the line of code Connector.open() pop up sometimes a screen that ask for your permision and you need a thread to listen to the user commands and another thread doing the net request (this task if permorfed by the thread that executes de Connector.open ()). Well apart from this, i sugges to do the following...

try to wait until the thread finish using a join message. I don't if PersonEntity extends from Thread. Join message is used to wait until the object finish his thread operations.

public void commandAction(Command command, Displayable displayable) {

// Insert global pre-action code here

if (displayable == formEnter) {

if (command == commandOK1) {

// Insert pre-action code here

PersonEntity person = new PersonEntity();

person.setId(Integer.parseInt(IdTextField.getString()));

person.start(); //initiates run()

getDisplay().setCurrent(get_formWelcome());

// Insert post-action code here

[b]person.join (); // try waiting for the thread to end, and then make other operations...[/b]

System.out.print("I'm Executing before Thread finishes");

/*line-->*/ FrmWelStringItem.setText(person.getName); /*This line should execute After Thread finishes but doesn't*/

}

Hope that help!

Vituchina at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Hello MR.

First of all, thank you very much for the kind reply.

ok, PersonEntity Does extend from Thread, and you can call the join() method on it.

However, and unfortunately, I have tried that... and the result is that it makes the application Freeze when the screen that asks permission for network usage appears.

I don't know what else to do. Could you please continue helping me?

Or in general, Could anybody, please please help me, I am sure someone certainly has already solved this....

Thanks in advance.

Keep up with Fabulous work!

Sincerely, J

BCE_Josea at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

unfortunately join() is not going to work for you in this case. The problem with using join() with your current design, is that by calling it, you're having your "main/UI" thread wait for the other thread to complete. Which means, it can't continue executing, and can't take any more UI events or user input... aka... freeze! :)

pandora_fooa at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Mr. pandora_foo.

Hello, thank you very much for your kind explanation.

I never could have thought there was a "main/UI" thread. That seems to be a Huge problem now.

Could you please advice me on how to solve my particular Scenario?

I really need to solve this.

any suggestion at this point is certainly appreciated, How can I make a Design that works?

Thank you very much sir, and of course to everybody who's reading.

Sincerely, J

BCE_Josea at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Well, you could always call some public method of class your commandlistener is in from withing the PersonEntity-class when it has finished doing what it is doing. Something like:

if (command == commandOK1) {

PersonEntity person = new PersonEntity(this);

}

and then in PersonEntity-class

public PersonEntity(parentclass c){

parent=c;

}

.

.

.

parent.myPublicMethod();

Zangaza at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

Hello There!

Zangaz, your way seems like the way to go...

Could you please sir, Explain further What did you mean with:

"call some public method of class your commandlistener is in"

In other words I would like to know what exactly is passed as a parameter when you pass this to the PersonEntity constructor in this case:

PersonEntity person = new PersonEntity(this);

I suppose it is the MIDlet itself your passing as a parameter?

And then, please (I know I'm probably a total ignorant for asking this), But I have no Clue of What does this line do:

parent.myPublicMethod();

neither do I know where to put it.

Excuse My ignorance,

Best Regards, and thank you very much for all the help!

J

BCE_Josea at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

Do it like this >>

public void commandAction(Command command, Displayable displayable) {

// Insert global pre-action code here

if (displayable == formEnter) {

if (command == commandOK1) {

// Insert pre-action code here

final PersonEntity person = new PersonEntity();

person.setId(Integer.parseInt(IdTextField.getString()));

new Thread (new Runnable () {

public void run () {

person.Communicate (); // say the name of ur method in PersonEntity which describes the process u are now doing in the run method of PersonEntity.

getDisplay().setCurrent(get_formWelcome());

// Insert post-action code here

get_FrmWelStringItem().setText (person.getName);

}

}).start ();

}

Or else for a better solution, create an Event Listener for the actions u are doing in PersonEntity class. Implement the Listener in ur main class, means the one in which u are creating the objects of PersonEntity. Do things accordingly in the implemented methods of the EventListener.

find_suvro@SDNa at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

example:

public class MyMidlet extends MIDlet implements CommandListener {

public void startApp() {

//do stuff

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

public void commandAction(Command command, Displayable displayable) {

//do stuff

PersonEntity person = new PersonEntity(this);

//do more stuff

}

public void myOwnMethod(){

//do stuff you want done after httpconnection

}

and your PersonEntity class looks like this

public class PersonEntity{

//stuff

private MyMidlet parent=null;

public PersonEntity(MyMidlet m) {

parent=m;

}

//more stuff

}

Then, when you have line:

parent.myOwnMethod();

in your PersonEntity class the method "myOwnMethod" of MyMidlet is called. Of course you can do that listener stuff mentioned above if you feel your up to it.

Zangaza at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

Hi There!

Yes, now it's working! and even seems to be respoding quicker than before.

I totally get it now, by passing the Parent Class (MIDlet) I can make all the code inside the Thread finish before going to the next statement.

N I C E ! thank you Zangaz.!

I Would really like to know how to create Event Listeners, because at the moment I don't seem to understand How do they theorically work, and that seems like a more standard way of doing this process.

thanks for your help find_suvro@SDN, I tried your solution and it works too, Thank you very much! I have learned so much ! You can't possibly imagine how thankful I am.

God Bless you.

Sincerely,

Jose.

BCE_Josea at 2007-7-28 18:56:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...