Researched by Ed Ort
April 2002
Question
How do I open and reuse network connections in J2ME CLDC/MIDP?
Answer
MIDP 1.0 supports only the HttpConnection. General purpose socket connections are beyond the scope of the official specification. New network protocols are specified at JSR 118 MIDP NG currently in public review. The CLDC GCF is intended for single session or transaction use of I/O connections, so HttpConnection supports only a single request/response transaction with a web server.
The Connection object itself allows you to set up request parameters and access response codes and headers independently of the basic I/O operations. The StreamConnection combines an InputConnection and an OutputConnection, but the actual InputStream and OutputStream are independent aspects of the connection that are opened and closed separately.
Here's an example of opening and closing a connection stream:
StreamConnection c = (StreamConnection)Connector.open("http://example.com/");
InputStream s = c.openInputStream();
while ((ch = s.read()) != -1) {
...
}
s.close();
c.close():
|
Because security checks are likely to take place on the connection setup, it is likely that an official socket connection would also be defined to deal with single interactions with a server. For example, you would not be allowed to close and reopen streams associated with a socket connection.
One of the original goals of "generic" connections, was to allow writing applications in a transport independent way, thus allowing the provisioning of applications to require simple configuration of connection strings. In the web world people already understand that a browser would simply GET and display file://foo.txt or http://example.com/foo.txt.
This could translate into applications built with a property in the application descriptor file to indicate where data would be fetched, so that the Connector.openInputStream(getAppProperty("MyConnection")) would allow deployment portals to simply change a URL string in the descriptor file to customize the application behavior. e.g. https://example.com/foo.txt or socket://example.com:12345
Acknowledgments
Thanks to Gary Adams for answering this question.
Back To Top
|