Introduction of J2ME Universal Networking framework

Source: Internet
Author: User
Tags socket

The universal networking framework plays a very important role in the J2ME platform, and without it, the applications we write can only be "mystical", and the application of a common networking framework for networking is "magical". This article will be the general networking framework for the overall analysis, the specific application will be detailed in the subsequent articles.

The Generic Connection Framework (hereinafter referred to as GCF) is defined in CLDC, and because of the resource-constrained nature of Mobile information devices, java.net and java.io packages are not suitable for use here. The real world uses a variety of network communication protocols, which makes it very difficult to design this framework, at least I think so. However, when we analyze GCF it is clear that it has the following characteristics: interface design, easy to expand, provide the factory method to create the connection, use the standard URL to simplify the work of the programmer. When we look at the CLDC1.1 API, we can see that it defines 8 interfaces, a connector class, and a Connectionnotfoundexception exception. Extended in MIDP2.0, providing httpconnection, httpsconnection interfaces, so that MIDlet has the ability to communicate with the server via HTTP or HTTPS protocol, Provides a socketconnection, serversocketconnection, udpdatagramconnection interface that enables MIDlet to tcp/ The ability of the IP layer to communicate through a socket or use datagrams to communicate. As for commconnection and push techniques are not described here.

Combining GCF's interface hierarchy diagram to understand the common networking framework is the best way, let's look at its hierarchy.

The top-level interface is connection, and the other interfaces are inherited from him. Only one method, close (), is defined in connection. In our real world, we usually use packet data exchange and circuit switching, so we define the datagramconnection and Streamconnection in the networking framework accordingly. Because we need to operate on both input and output flows based on streaming, Streamconnection extends Inputconnection and Outputconneciton, The Conn.openinputstream (), Conn.openoutputstream () methods that we often use are defined in these two important interfaces. The Streamconnectionnotifier interface defines the capabilities that a connection listener should have, and its method Acceptandopen () method returns a streamconnection type of connection, Serversocketconnection inherits the Streamconnectionnotifier interface so that if you do the socket server, you can use this url,socket://: Port establishes a listening port on your device to wait for a connection. SocketConnection inherited streamconnection just can and serversocketconnection each other. Udpdatagramconnection is for use in packet data interchange, and he inherits the Datagramconnection interface. There are only three methods defined in the Contentconnection interface GetEncoding (), GetLength () and GetType (), and the httpconnection that we are very familiar with is his subclass, A large number of operations are defined in Httpconnection, and HTTP networking is also the way in which the MIDP specification requires vendors to support them. Now you should be more clear about the level, and continue to look down how to use GCF.

GCF is very simple to use, mainly focused on the connector open () method. All we have to do is provide a standard URL parameter passed to the open method, for example, to get a httpconnection we should write code similar to the following:

String url = “http://myip:myport/myservlet”;
HttpConnection httpConn = (HttpConnection)Connector.open(url);

We should know how to define the format of this URL, if you are interested you can refer to RFC2396, I only include his basic format: {scheme}:[{target}][{parms}], for different ways of network communication, you have to do is write a different URL, And by casting to get the type of connection you need. This article does not provide information on how to conduct data transfer, you can refer to the Httpconnection interface in the MIDP API documentation for detailed instructions, or you can refer to the development of the J2ME networking application, and do not forget to close the connection after the data transfer is complete. You should then use the close () method defined in connection.

try {
  // Here close any open streams
  conn.close();
  } catch (IOException ioe) {
  // Handle the exception.
  // Throw the exception, ignore it, or return an error.
  }

So far, the overall introduction of GCF is over. We should be aware of the GCF interface hierarchy and how to use GCF. And don't forget to keep the design of this framework in mind, in fact I think many of the designs in CLDC and MIDP deserve a lot of thinking.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.