用J2ME的通用联网框架开辟联网操纵程序[Java编程]
本文“用J2ME的通用联网框架开辟联网操纵程序[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
手机联网给开辟人员不小的震动的.毕竟这真的是件奇异的事情,不是吗?本文将报告若何利用J2ME平台中的通用联网框架开辟联网的利用程序.
首先,必须阐明一点:MIDP中规定,任何移动信息设备都必须供应通过http协议的支持,而像其他的通信方法比方socket是设备相关的.有些手机会支持,有些则不支持.这里只大约的阐明一下http协议相关的内容,假如不理解这个方面的知识请参考http协议.在javax.microedition.io里面是大量的接口,只有一个connector类,当然在midp2.0里面增添了对push技术的支持,这个留做今后讲.connector类供应的最重要的办法是open()办法,它的返回值为Connection,你可以对他举行转换得到你需求的范例,比方我们以http协议拜候服务器.
void postViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
try {
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write("LIST games
".getBytes());
os.flush(); // Optional, getResponseCode will flush
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
processType(type);
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}
以上是“用J2ME的通用联网框架开辟联网操纵程序[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |