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
New Developer
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Still getting Blocking Operation not permitted on event dispatch thread

He there,

 

Im new to JDE so i hope you can help me out. I am writing an application which retrieves data from an website and displays its result in

a richtTextField on the MainScreen. But when it calls the function rTextField.setText(result_data) then it keeps comming up whith this

irritating error: "Blocking operation not permitted on event dispatch thread" Here is my code:

 

package bbApp; import net.rim.device.api.ui.*; import net.rim.device.api.ui.MenuItem; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import net.rim.device.api.io.*; import java.io.*; import java.lang.*; import javax.microedition.io.*; public class bbApp extends UiApplication { public bbApp() { pushScreen(new frmMain()); } public static void main(String[] args) { bbApp theApp = new bbApp(); theApp.enterEventDispatcher(); } static class frmMain extends MainScreen { private RichTextField rTekst; public frmMain() { super(); setTitle("EZ Planner"); rTekst = new RichTextField("Hier komt de data in."); add(rTekst); } public boolean onClose() { System.exit(0); return true; } private void readData() { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { StreamConnection conn; int ch; StringBuffer data; try { data = new StringBuffer(); conn = (StreamConnection)Connector.open("http://www.bodeva.nl/sitemap.xml"); InputStreamReader r = new InputStreamReader(conn.openInputStream()); while ( (ch=r.read()) > -1 ) { data.append((char)ch); } conn.close(); r.close(); rTekst.setText(data.toString()); } catch(Exception e) { rTekst.setText(e.toString()); } } }); } protected void makeMenu(Menu menu, int instance) { MenuItem mData = new MenuItem("Data ophalen",1,50){ public void run() { readData(); } }; MenuItem mClose = new MenuItem("Sluiten",1,50){ public void run() { onClose(); } }; menu.add(mClose); menu.add(mData); } } }

 

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Developer
Posts: 16,851
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Still getting Blocking Operation not permitted on event dispatch thread

you try to run your data operation on the event thread, that is exactly the thing you should not do Smiley Happy

put your connection stuff into an own thread, most simple by new Thread(Callback callback){public void run(){//code here}}.start();
----------------------------------------------------------
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
New Developer
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Re: Still getting Blocking Operation not permitted on event dispatch thread

Can you help me out al little with a code sample?

 

I have this dataReader in an separate class called clsData with a void calld getData

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
New Developer
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Re: Still getting Blocking Operation not permitted on event dispatch thread

[ Edited ]

For those with the samen problem i had.

1. class called clsData 

2. class called frmMain (MainScreen)

3. class called bbApp (UiApplication)

 

 the clsData class:

//clsData class import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import net.rim.device.api.io.*; import java.io.*; import java.lang.*; import javax.microedition.io.*; public class clsData { public clsData() { super(); } public String getData() { StreamConnection conn; int ch; StringBuffer data; try { data = new StringBuffer(); conn = (StreamConnection)Connector.open("http://www.yoururl.com/sitemap.xml"); InputStreamReader r = new InputStreamReader(conn.openInputStream()); while ( (ch=r.read()) > -1 ) { data.append((char)ch); } conn.close(); r.close(); return data.toString(); } catch(Exception e) { return e.toString(); } } }

 

the frmMain class:

 

//frmMain class import net.rim.device.api.ui.*; import net.rim.device.api.ui.MenuItem; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; final class frmMain extends MainScreen { private RichTextField rTekst; public frmMain() { super(); setTitle("BlackBerry App"); rTekst = new RichTextField("Here go's the data."); add(rTekst); } public void updateTekst(final String tekst) { UiApplication.getUiApplication().invokeLater (new Runnable() { public void run() { rTekst.setText(tekst); add(new SeparatorField()); invalidate(); } }); } public boolean onClose() { System.exit(0); return true; } protected void makeMenu(Menu menu, int instance) { MenuItem mData = new MenuItem("Data ophalen",1,50){ public void run() { Thread thData = new Thread(new Runnable(){ public void run() { clsData c = new clsData(); updateTekst(c.getData()); } }); thData.start(); } }; MenuItem mClose = new MenuItem("Sluiten",1,50){ public void run() { onClose(); } }; menu.add(mClose); menu.add(mData); } }

 

 

and the bbApp class:

//bbApp class import net.rim.device.api.ui.*; import net.rim.device.api.ui.MenuItem; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import net.rim.device.api.io.*; import java.io.*; import java.lang.*; import javax.microedition.io.*; public class bbApp extends UiApplication { public bbApp() { pushScreen(new frmMain()); } public static void main(String[] args) { bbApp theApp = new bbApp(); theApp.enterEventDispatcher(); } }

 

Message Edited by digitalhuman on 10-28-2008 12:32 PM
Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Highlighted
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Still getting Blocking Operation not permitted on event dispatch thread

Depending on which JDE you have, there should be plenty of example of http IO and UI component usage.

You should see something in there about making new threads but it should be easy to cut/paste the code.

I'd also recommend the Sun website for general java tutorials on UI threading models and threads in general,

 

http://www.google.com/search?hl=en&fkt=1552&fsdt=9784&q=site%3Asun.com+thread+tutorial+awt&aq=f&oq=

 

whch oddly enough turned up a top hit on on of my recent rants, something you may run into later, deadlock due

to glib over use of  "synchronized for safety",

 

http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/upgrade-guide/article-07.html

 

[ this is also funny becomes it takes a slam at the MS JVM, and I can't actually remember now whose libraries, Sun or MS, were

originally over-synchronized but earlier I blamed Sun when it may very well have been MS. Community gossip fwiw ]