Some devices do not allow multiple network connections to be turned on at the same time, but sometimes our applications may need to open multiple connections at the same time. This can improve efficiency or the user experience, but the application should try to avoid relying on a multiple connection feature because your program might crash for devices that do not support multiple connections.
The solution to this problem is to mark the number of connections that the device can open simultaneously in the Jad file.
So how to get the maximum number of connections supported by the device, first you can refer to the specification of the device. For example, Motorola's device specification generally lists the types of connections that can be supported, such as HTTP sockets. And the maximum number of connections that the device can open at the same time. For what is not stated in the specification, we can only write a test MIDlet to test this feature first. Then write him to the Jad file for later use.
For example, the number of maximum connections is marked in the following Jad file.
MIDlet-1: Foo, , Foo
MIDlet-Jar-Size: 7063
MIDlet-Jar-URL: foo.jar
MIDlet-Name: Foo
MIDlet-Vendor: Yoyodyne Inc.
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0
Device-Max-Connection: 3
This quantity can be read when the application is initialized, and note that this quantity should be stored as a global variable so that it is accessible to people. Initialize the work as follows,
/* Initialization code */
String deviceMaxConnectionString = getAppProperty
("Device-Max-Connection");
int deviceMaxConnection = Integer.parseInt
(deviceMaxConnectionString);
int openConnection = 0;
同时创建多个网路连接,如下:
if (openConnection < deviceMaxConnection) {
HttpConnection connection = (HttpConnection) Connector.open
(uri);
openConnection++;
} else {
// Fail to create connection.
}
Turn off network connections
Connection.close (); openconnection--;
Calculates the number of connections,
int connection = 0;
try {
while (true) {
Connector.open("http://tools.ietf.org/html/rfc262"
+ connection);
}
} catch (Exception e) {
// Max number reached here
}