Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deeply understand J2ME network programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to deeply understand J2ME network programming, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Focus on J2ME network programming. This structure in the javax.microedition.io package includes the Connection class and several useful interfaces (including Stream Connection, ContentConnection and HTTPConnection). The editor discusses the design of this package and uses the Stream Connection and ContentConnection interfaces to enhance the functionality of the address bookMIDLet example introduced earlier.

J2ME network programming

Earlier I introduced the development of local device data storage through the record Management system (RMS). Another important feature of J2ME is the ability to open network connections and transfer data using the J2ME connection structure. This structure in the javax.microedition.io package includes the Connection class and several useful interfaces (including Stream Connection, ContentConnection, and HTTPConnection). This article discusses the design of this package and uses the Stream Connection and ContentConnection interfaces to enhance the functionality of the address bookMIDLet example described earlier.

1. Javax.microedition.io package

If you have experience developing programs using the J2SEjava.net package, you will know that it is widely used and provides some very advanced network performance. Unfortunately, due to the size of the device memory, these advanced features are not suitable for limited connection devices to configure CLDC. To make up for it, we have a simplified but fully functional connection structure that allows a simple connection to transfer data. The MID profile goes a step further by defining a HTTPConnection interface for HTTP access on the network.

2. Modify Address Book MIDLet

The routines in this section are almost the same as the record management system address book routines in the previous section, where the example uses a local data file, while the example I'm going to introduce now uses the J2ME network function to retrieve an address from a text file stored on the Internet. This text file is called address book.txt, and the names and addresses in the file are separated by commas. As I mentioned earlier, the following example uses two different J2ME interfaces to perform data transfer: Stream Connection and Content Connection.

Use Stream Connection to access data

The Stream Connection interface defines the minimum functionality that a stream connection must have. Now let's make changes to the address bookMIDLet application:

Delete the dbAddress.addAddress () method call from the address bookMIDLet () constructor, which can be deleted because there is no need for the new program to add data to the database on its own, and the new program will use the network features of J2ME to retrieve the address stored on the Internet.

Add specific connection code to the AddressDB constructor. The specific connection code in both examples simply retrieves the address through TCP/IP and manually adds each address to the address book.

StreamConnection connStream=null;InputStreaminStream=null; byte [] b=newbyte [255]; Stringaddress,name; intcommalocation=0; try {connStream= (StreamConnection) Connector.open ("http://localhost/address book.txt"); inStream=connStream.openInputStream (); intcount=inStream.read (b); address=newString (b); addressaddress=address.trim (); StringTokenizerst=newStringTokenizer (address, "); while (st.hasMoreTokens ()) {address=st.nextToken (); commalocation=address.indexOf (','); name=address.substring (zero Commalocation); addressaddress=address.substring (commalocation+1) AddAddress (name,address);}} catch (IOExceptione) {System.out.println (e); e.printStackTrace ();}

The above code works, but there is a slight problem. The length of the byte array must be limited to 255 because the StreamConnection interface cannot estimate the size of the downloaded data. To do this, I can take advantage of the ContentConnection interface and the well-used getLength () method.

Another problem is the lack of J2SE's java.util.StringTokenizer class in J2ME. Although you can write your own string parser class, you can also find the StringTokenizer class on the Internet.

On how to in-depth understanding of J2ME network programming to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report