Although the current wireless network is not ideal, the mobile phone network is still shocking to our developers. After all, this is true.
It's a magic thing, isn't it? This article describes how to use the universal networking framework in the j2-based platform to develop a networked application.
Program.
First, it must be noted that, in MIDP, any mobile information device must provide support through http
While other communication methods such as socket are device-related. Some mobile phones are supported, while others are not. Here only
Briefly describe the content related to the http protocol. If you do not know this knowledge, refer to the http protocol. In
Javax. microedition. io contains a large number of interfaces, only one connector class, of course, added in midp2.0
The support for push technology will be discussed later. The most important method provided by the connector class is the open () method. Its
The returned value is Connection. You can convert it to get the type you need. For example, we access the service through http.
.
Void postViaHttpConnection (String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream OS = null;
Int rc;
Try {
C = (HttpConnection) Connector. open (url );
// Set the request method and headers
C. setRequestMethod (HttpConnection. POST );
C. setRequestProperty ("If-Modified-Since", "29 Oct 1999 19:43:31 GMT ");
C. setRequestProperty ("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0 ");
C. setRequestProperty ("Content-Language", "en-US ");
// Getting the output stream may flush the headers
OS = c. openOutputStream ();
OS. write ("LIST games". getBytes ());
OS. flush (); // Optional, getResponseCode will flush
// Getting the response code will open the connection,
// Send the request, and read the HTTP response headers.
// The headers are stored until requested.
Rc = c. getResponseCode ();
If (rc! = HttpConnection. HTTP_ OK ){
Throw new IOException ("HTTP response code:" + rc );
}
Is = c. openInputStream ();
// Get the ContentType
String type = c. getType ();
ProcessType (type );