Although the current wireless network is not ideal, the mobile phone network is not shocked by our developers. After all, this is true.
Is a magical thing, isn't it? This article will tell the application of the Universal Internet Framework Development Networking in the J2ME platform.
program.
First, you must explain a point: MIDP, any mobile information device must provide a support through the HTTP protocol
Hold, as other communication methods such as Sockets are related to the device. Some mobile phones will be supported, some do not support. Here only
Probably illustrates the content related to the HTTP protocol. If you don't understand this knowledge, please refer to the HTTP protocol. in
Javax.microedition.IO is a large number of interfaces, only one Connector class, of course, adding it in MIDP2.0.
Support for Push technology, this will be explained later. The most important way to provide the Connector class is open () method, it
The return value is Connection, you can convert him to get the type you need, such as access to the HTTP protocol
.
Void PostViahttpConnection (String Url) throws oException {
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 Conifiguration / CLDC-1.0");
C.SetRequestProperty ("Content-Language", "EN-US");
// Getting the Output Stream May Flush The Headers
Os = c.OpenOutputStream ();
Os.write ("List games / n" .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 oException ("HTTP Response Code: RC);
}
IS = C.OpenInputStream ();
// Get the contenttype
String type = c.gettype ();
Processtype (TYPE);
// Get the length and process the data
INT LEN = (int) c.getlength ();
IF (len> 0) {
INT ACTUAL = 0;
Int bytesread = 0; byte [] data = new byte [len];
While ((BytesRead! = LEN) && (actual! = -1)) {
Actual = is.read (data, bytesread, len - bytesread);
BYTESREAD = Actual;
}
Process (data);
} else {
int CH;
While ((ch = is.read ())! = -1) {
PROCESS (BYTE) CH);
}
}
} catch (classcastexception e) {
Throw New IllegalargumentException ("Not an HTTP URL");
} finally {
IF (is! = null)
Is.close ();
IF (OS! = NULL)
Os.Close ();
IF (c! = null)
C. close ();
}
}
The above code is I taken from the API DOC (it is recommended to read the API DOC).
Below, according to your own experience, it is important to the net network:
We should understand how this works, and the phone sends a request to transfer to the operator's WAP gateway through the wireless network.
The WAP gateway forwards the request to the web server, and the server can be built with CGI, ASP, Servlet / JSP. After the server is processed
The response will be forwarded to the WAP gateway, and the WAP gateway will send it to the phone. WAP gateway is for our developers
Transparent we don't need to manage it.
If you can't see Thread, runnable in your netbook, then your program cannot run
of. Since the factors considering the network, in order to avoid operational jamming. You must put the networked action to another thread
OK, not running in the main thread. It is best to provide a waiting interface for the user when it is networked, such as an animated world.
surface. I am not used in the examples provided below, because I want to talk about this separately.
Usually the interface of the networked application is more, it is best to use the MVC mode to implement the navigation of the interface.
Consider how you want to pass your data, this is very important. You can use the GET method to use the Post method,
Recommended latter. Because the GET method can only be transmitted through the URL encoding. And POST is more flexible, cooperated
DataInputStream, DataOutputStream is more convenient to use. It must be clear how we accept data is a follower
According to how to send it, for example, in the Client side Writeutf (Message); WriteinT (4); WriteBoolean
(TRUE), then accept should readf (); readint (); readboolean (); if the transmitted data length is available
So we can build an appropriate array to accept, if we don't use us to pick up an appropriate capacity array
Affected.
When I netwary, I must do it in the following process.
1. Establish a connection, set the transfer method recommended POST, set the method head
2, turn on the output stream, transfer the data to the server
3. Judging the corresponding status code, enter different flow control, pay attention to the error handling. If OK starts accepting data
4, turn off the connection and stream