This article will focus on the design of the network program for j2_vpc. 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 functions of the address bookMIDLet example described earlier.
Network Program Design
Previously, I introduced the development of local device data storage through the record management system (RMS). Another important feature of j2's is the ability to use the j2's connection structure to open network connections and transmit data. 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 functions of the address bookMIDLet example described earlier.
1. javax. microedition. io package
If you have experience using J2SEjava.net package development programs, you will know that it is widely used and provides some very advanced network performance. Unfortunately, due to the memory size of the device, these advanced features are not suitable for configuring CLDC for a limited connection device. To make up for this, we have a simplified but fully functional connection structure that allows simple connections to transmit data. The MID short table has a step forward. It also defines an HTTPConnection interface for HTTP access on the network.
2. Modify the Address Book MIDLet
The routines in this section are almost the same as those in the address book routine of the Record Management System in the previous section. The examples in the previous section use local data files, now I want to introduce the example of using the network function of J2EE to retrieve an address from a text file stored on the Internet. The text file name is address book.txt, and the names and addresses in the file are separated by commas. As mentioned above, the following example uses two different interfaces, Stream Connection and Content Connection, to transmit data.
3. Use Stream Connection to access data
The Stream Connection interface defines the minimum function required by a Stream Connection. Now let's modify the address bookMIDLet application:
Delete dbAddress in address bookMIDLet () constructor. addAddress () method call. This method call can be deleted because the new program does not have to add data to the database, the new program will use the network function of j2m's to retrieve the address stored on the Internet.
Add the specific connection code to the AddressDB constructor. The specific connection code in the two 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(0,commalocation);
- addressaddress=address.substring(commalocation+1);
- addAddress(name,address);
- }
- }
- catch(IOExceptione){
- System.out.println(e);
- e.printStackTrace();
- }
-
The above code can work, but it has a small 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 this end, I can use the ContentConnection interface and the getLength () method that is very useful.
Another problem is that J2SE java. util. StringTokenizer class is missing in J2SE. Although you can write the string analyzer class by yourself, you can also find the StringTokenizer class on the Internet.