开发者

Blackberry HTTPConnection best practices

I am developing a project for BB. The application works with the network and sends / receives data via HTTP. Now I use the queue and queue manager. Manager starts with a background thread and works in while (true) loop, checking the queue for new transactions to the server. If the queue is not empty, then the transaction is executed, otherwise the manager goes to sleep for 200 ms.

The process of the transaction as follows: - Runs another thread (using the Runnable), which opens a connection to the network and first thread waiting for background thread or timeout (and for that we need a loop), which we set. - If the connection is established, then starts another thread (using the Runnable), which runs getResponseCode (), and first thread waiting for background thread or timeout (and for that we need a loop), which we set.

Before it, we showing popup window with wait-rotating-image, and after it is removed. It synchronized via Application.getEventLock ().

Iit unstable sometimes and thread sleeps for a long time ignore timeout-waiting-loop.

I would like to know how valid such an approach, what advice and best-practice is, what is your experience?

I use 开发者_运维问答4.5, 4.6, 4.7 and 5.0.


The lock returned by Application.getEventLock() should only be used for code that modifies the UI or UI components - it's the lock used by the event dispatcher. You should not be using it for background tasks such as HTTP processing. If you want to synchronize that code, it would be best to just create your own lock object.


You do not need that many threads, your EDT (event dispatch thread a.k.a main thread) should insert he job (some runnable class) into a queue and use wait/notify to inform a dedicated worker thread, that is responsible for network transaction, to check the queue. The worker thread will be responsible for opening connection, writing to connection and reading from it.

For information about wait/notify mechanism check out: A simple scenario using wait() and notify() in java

Due to the fact that you can't update the UI using the worker thread, Once the network transaction is completed you can update the UI layer using InvokeLater

For more details go to http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/system/Application.html#invokeLater(java.lang.Runnable)

you can set a timeout in the HTTPConnection itself, but if you don't want to rely on that mechanism, you can schedule a TimerTask that will execute after some time and handle the timeout in case no response is received. Once the response is received all you need to do is cancel the TimerTask so that the timeout will not be triggered.

Check out http://www.blackberry.com/developers/docs/4.0api/java/util/TimerTask.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜