Introduction
J2ME (Java 2 Micro Edition) is an integral part of Java 2, and it is called J2SE, EE. J2ME is a highly optimized Java operating environment, mainly for consumer electronics, such as cellular telephones, videophone, digital set-top boxes, and car navigation systems. The J2ME is a Java-specific version tailored for consumer electronics and handheld devices.
The advent of J2ME has made it possible to develop applications for Cross-platform consumer electronic products. Platform-Independent features of the Java language are ported to small electronic devices to allow shared applications between mobile wireless devices. It provides an advanced Internet protocol based on HTTP that enables mobile phones to access all the information directly to the Internet in a client/server manner, allowing different client access to different resources.
In the future wireless communication era, a large number of network applications will be developed to meet the requirements of wireless mobile communication, but to fully play the wireless mobile communication equipment communication capabilities, J2ME network programming has become particularly important. So in order to efficiently network programming, you need to take advantage of the Java language Multithreaded programming mechanism.
J2ME Network Connectivity Framework (GCF)
The Universal Connection Framework (Generic Connection FRAMEWORK,GCF) provides an extensible, common I/O framework for resource-limited devices. GCF is a set of interfaces defined in the Javax.microedition.io package. Figure 1 shows the class hierarchy of the GCF.
Figure 1 class hierarchy of GCF
There are seven interfaces defined in GCF, where Connection is the most basic type of connection. It also provides support for packet and stream connections. The interfaces that provide more functionality are derived down the hierarchy. For example, the Streamconnection interface supports both input and output streams, and the Contentconnection interface extends the Streamconnection interface to support the determination of the content type, data length, and encoding format of the convection. The Httpconnection interface extends the Contentconnection interface to support standard HTTP requests. A Mobile Information device framework, as defined in the framework layer for mobile or two-way pagers, in its MIDP 1.0 specification only requires the device to support the HTTP connection protocol, while the updated MIDP 2.0 specification requires simultaneous support for the information HTTP and HTTPS, which provides support for more secure network connections.
Multithreading in Network programming
Because the target device has the characteristics of small memory, weak computational ability and battery power, so how to make your application run efficiently is a big problem in development. Especially for mobile devices such as Mobile information equipment, wireless communication characteristics of our program put forward higher requirements. From the angle of code optimization, in network programming It is very important to introduce multithreading.
When the program is running, the application Management Software (Application management software) First Initializes a midlet, and then calls its Startapp () method to make the midlet into the active state, which is the main thread, the program branch. It performs other methods and then returns to this branch to continue execution. However, a network connection is a potentially clogged operation, meaning it may not return for long periods of time.
While simulating a segment of the network connection program running in Sun's wireless development Kit WTK, WTK prompts the network connection to work to plug user input and create another thread for networking. For the above situation, the introduction of multithreading processing mechanism.
1. Using thread class and Runnable interface
When you write a J2ME network-connected application, you often use the command display widget to invoke its event handler function to complete the network connection work, the code framework is as follows:
public void commandAction(Command c, Displayable s) {
if(c==sendCommand){
requestConnect();//连接方法
}
else if(
c==backCommand){
display.setCurrent(mainForm); }
else{
destroyApp(false);
notifyDestroyed(); }
}
// 获取一个HTTP的连接
private void requestConnect() {
String url= URL.URLString
HttpConnection hpc = null;
try{
hpc = (HttpConnection)Connector.open(url);
int status = hpc.getResponseCode();
if(status != HttpConnection.HTTP_OK)
content = "联机失败!";
else
content = "已联机!";
}
catch(IOException e){System.out.println(content);}
try{
if(hpc != null) hpc.close();
}
catch(IOException e2){}}