0

i developed an application that access the web through the mobile application using j2me for nokia cellphones but when i tried to run this application on blackberry devices i got a problem with it, no website want to be opened, it just freezes so can anyone help me pls cos i could not find any solution for it these r the lines of code that i'm using in the application

this line i'm using to read something from web

hc = (HttpConnection) Connector.open(url); dis = hc.openDataInputStream(); 
int dataleft = dis.available(); 
for (int j = 0; j < dataleft; j++) {
     buffer.append((char) dis.read()); 
}
dis.close(); 
hc.close();

and this line to open a website

this.platformRequest("http://stackoverflow.com/questions");
bharath
  • 14,283
  • 16
  • 57
  • 95
Hesham
  • 17
  • 4

1 Answers1

0

You're probably running that code (which has some issues... don't depend on available() to be accurate, you should just call read() until it returns -1) on the event thread, which is a big no-no on the BlackBerry. The app is likely trying to prompt the user for permission to make the HTTP request, but since the even thread is blocked it can't do it. There's a pretty good description of what to do and not do on the BlackBerry event thread here:

http://www.thinkingblackberry.com/archives/182

It's also mentioned in the API documentation for HttpConnection:

This interface performs blocking Input and Output operations. An application will lock if an implementation of this interface opens a connection from within the main event thread. Prevent an application from locking by opening a connection from within a thread that is separate from the main event thread.

The API docs are at:

http://www.blackberry.com/developers/docs/4.5.0api/javax/microedition/io/HttpConnection.html

Eric Giguere
  • 3,495
  • 15
  • 11
  • so i guess the problem can be solved if i solve the thread problem – Hesham Feb 07 '11 at 14:27
  • i tried all the thread solutions but seems to be hopeless, so would u plz tell me how can i solve the thread problem ?? – Hesham Feb 07 '11 at 14:29
  • It's really hard to say without seeing any code. But you should definitely make sure that you are calling Connector.open() on a separate thread. You should also be sure to trap exceptions and so on to make sure that the code is working as you expect. – Eric Giguere Feb 07 '11 at 14:36
  • its working just fine very well with nokia mobiles but i dont know what wrong with blackberry – Hesham Feb 07 '11 at 14:44
  • Is all your code on a single thread? If so, that's definitely the problem on the BlackBerry. – Eric Giguere Feb 07 '11 at 14:46
  • try { buffer.setLength(0); HttpConnection hc = null; DataInputStream dis = null; hc = (HttpConnection) Connector.open(url); dis = hc.openDataInputStream(); int dataleft = dis.available(); for (int j = 0; j < dataleft; j++) { buffer.append((char) dis.read()); } dis.close(); hc.close(); } catch (IOException e) { System.err.println("Error while connectiong to API!"); e.printStackTrace(); } – Hesham Feb 07 '11 at 14:48
  • You're not understanding. Are you running that code on a separate thread or not? If you don't understand threads, you should learn about those before doing anything more on the BlackBerry. – Eric Giguere Feb 07 '11 at 14:49
  • look i'll describe the program for u, first when u open the app it asks for a country and a phone number then i send them to the web page that checks the availability of those data and returns 1 or 0, thats all and if its available when i log into the application there r some buttons that takes me to a web page with opening a web browser – Hesham Feb 07 '11 at 14:52
  • 1st problme is with sending those data to the web and 2nd problem is when i open the web browser – Hesham Feb 07 '11 at 14:53
  • yea maybe u r right i should learn some about threads, thank you anyways for your help :) – Hesham Feb 07 '11 at 14:55