There are two GPRS network access points on J2ME, one is CMNET, one is CMWAP. Generally, the dream network, the treasure chest is hanging on CMWAP.
Cmnet
CMNET is a network access point of the general internet. We generally see the J2ME instance code you see there is a network download based on a CMNET access point. In general, CMNET can connect all network sites. Below is the code snippet of my download resource package in the development of the cmnet of the fairy.
/ ************************************************** **********************************
* Download network files
* @Param URL String Address URL of the file to download
* @Return Byte [] If downloaded successfully, return the byte buffer of the file;
* If the download failed, return NULL
* /
Public Byte [] Download_CMNET (String URL) {
CONTENTCONNECTION C;
InputStream IS = NULL;
BYTE [] DATA = NULL;
Try {
C = (ContentConnection) Connector.open ("http: //" servername "/" URL, Connector.Read, true);
IS = C.OpenInputStream ();
INT DATALENGTH = (int) c.getlength ();
IF (datalength == -1) {
ByteaRrayoutputstream bstrm = new byteArrayoutputStream ();
int CH;
While ((ch = is.read ())! = -1)
BSTRM.WRITE (CH);
Data = BSTRM.TOBYTEARRAY ();
BSTrm.Close ();
}
Else {
Data = new byte [datalength];
Data_read_buf (IS, DATA, 0, DATALENGTH);
}
Is.close ();
C. close ();
} catch (exception e) {
Data = NULL;
}
C = NULL;
Return Data;
}
The code is very common, but it is necessary to pay attention to the following two points.
1. GetLength () is not necessarily effective, such as I found this function on the mobile phone in Nokia 40, but on the mobile phone of Nokia 60, this function is usually invalid. So we usually have another download method, both from the While loop constantly from the input stream, one read byte, if the read is -1, indicating that the input stream data is read.
2. The last parameter of Connector.Open is to indicate whether Timeout is allowed, usually we have to set true, because the GPRS network is often interrupted, then there must be a Timeout to exit the connection. According to my experience, if you can perform C.OpenInputStream (), it indicates that the connection is connected. As long as the connection is connected, the data download is faster (that is, the network connection is the slowest).
CMWAP
Moto's mobile phones are usually the default access point is CMWAP. The default access point can be set in the "Web" -> "Web Set" on your phone. Generally, the MOTO mobile phone in mainland China is a network access point of the "Mobile Dream Network", and the start is the CMWAP access point. The most evil thing in MOTO is that once the default access point is CMWAP, all J2ME applications cannot access the CMNET. NOKIA 40 Although the default access point is CMWAP, the CMNET can be automatically checked in the operation. There are two ways to solve MOTO network connections:
1. Create a web page in the "Web" -> "Web Set", then you don't need to fill in any parameters, set it to the default, it can allow your mobile phone to access the network through the CMNET.
2. Access the Internet via CMWAP through the mobile agent. Below is the code snippet using CMWAP download resource pack in Xianjian:
/ ************************************************** **********************************
* Download network files
* @Param URL String Address URL of the file to download
* @Return Byte [] If downloaded successfully, return the byte buffer of the file;
* If the download failed, return NULL
* /
Public Byte [] Download_CMWAP (String URL) {
HttpConnection C;
InputStream IS = NULL;
BYTE [] DATA = NULL;
Try {
C = (httpConnection) Connector.open ("http://10.0.0.0.172:80/" URL, Connector.Read, true);
C.SetRequestProperty ("X-Online-Host", ServerName);
C.SetRequestProperty ("accept", "* / *");
IS = C.OpenInputStream ();
INT DATALENGTH = (int) c.getlength ();
IF (datalength == -1) {
ByteaRrayoutputstream bstrm = new byteArrayoutputStream ();
int CH;
While ((ch = is.read ())! = -1)
BSTRM.WRITE (CH);
Data = BSTRM.TOBYTEARRAY ();
BSTrm.Close ();
}
Else {
Data = new byte [datalength];
Data_read_buf (IS, DATA, 0, DATALENGTH);
}
Is.close ();
C. close ();
} catch (exception e) {
Data = NULL;
}
C = NULL;
Return Data;
}
For example, we want to download
Http://xxx.xxx.xxx.xxx/bb/aa.dat, then above ServerName = "xxx.xxx.xx.xxx", and url = "bb / aa.dat"
2004-11-18