开发J2ME联网应用程序
发布时间:2008/5/27 0:00:00 访问次数:425
首先,必须说明一点: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\n".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();
}
}
上面的代码是我取自api doc(建议多读一下api doc)。
下面根据自己的经验说明一下联网中比较重要的问题:
- 我们应该明白这是如何工作的,手机发送请求通过无线网络传输到运营商的wap网关,wap网关将请求转发到web服务器,服务器可以用cgi,asp,servlet/jsp等构建。服务器处理后会把响应转发到wap网关,wap网关再把它发送到手机上。wap网关对我们开发人员来说是透明的我们不用管它。
- 如果在你的联网程序上看不到thread,runnable这样的字眼,那么你的程序是不能运行的。因为考虑到网络的因素,为了避免操作堵塞。你必须把联网动作放到另外一个线程去运行,而不能在主线程运行。最好当联网的时候提供给用户一个等待的界面比如作一个动画界面。我下面提供的例子中没有用,因为我想把这个单独出来以后谈。
- 通常联网的应用程序的界面是比较多的,最好我们使用mvc的模式来实现界面的导航。关于这个方面的说明我以前有文章讲述
- 考虑好你想如何传递你数据,这一点是非常重要的。你可以用get方法也可以使用post方法,推荐后者。因为get方法只能是通过url编码的传输。而post更加灵活,配合datainputstream、dataoutputstream来使用更是方便。必须清楚我们如何接受数据是跟数据如何发送过来相关的,例如在client端writeutf(message);writeint(4);writeboolean(true),那么接受就应该readutf();readint();readboolean();如果发送过来数据长度是可用的,那么我们可以建立一个适当的数组来接受,如果不可用我们就要一个适当容量的数组来接受。
下面我提供一个实例来说明以上的问题,在应用程序中我们输入任意字符,通过连接server得到响应并显示出来。server我用servlet写的非常简单,只是在受到的内容后面加上“haha”,程序我安装到自己的手机上运行没有任何
首先,必须说明一点: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\n".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();
}
}
上面的代码是我取自api doc(建议多读一下api doc)。
下面根据自己的经验说明一下联网中比较重要的问题:
- 我们应该明白这是如何工作的,手机发送请求通过无线网络传输到运营商的wap网关,wap网关将请求转发到web服务器,服务器可以用cgi,asp,servlet/jsp等构建。服务器处理后会把响应转发到wap网关,wap网关再把它发送到手机上。wap网关对我们开发人员来说是透明的我们不用管它。
- 如果在你的联网程序上看不到thread,runnable这样的字眼,那么你的程序是不能运行的。因为考虑到网络的因素,为了避免操作堵塞。你必须把联网动作放到另外一个线程去运行,而不能在主线程运行。最好当联网的时候提供给用户一个等待的界面比如作一个动画界面。我下面提供的例子中没有用,因为我想把这个单独出来以后谈。
- 通常联网的应用程序的界面是比较多的,最好我们使用mvc的模式来实现界面的导航。关于这个方面的说明我以前有文章讲述
- 考虑好你想如何传递你数据,这一点是非常重要的。你可以用get方法也可以使用post方法,推荐后者。因为get方法只能是通过url编码的传输。而post更加灵活,配合datainputstream、dataoutputstream来使用更是方便。必须清楚我们如何接受数据是跟数据如何发送过来相关的,例如在client端writeutf(message);writeint(4);writeboolean(true),那么接受就应该readutf();readint();readboolean();如果发送过来数据长度是可用的,那么我们可以建立一个适当的数组来接受,如果不可用我们就要一个适当容量的数组来接受。
下面我提供一个实例来说明以上的问题,在应用程序中我们输入任意字符,通过连接server得到响应并显示出来。server我用servlet写的非常简单,只是在受到的内容后面加上“haha”,程序我安装到自己的手机上运行没有任何
上一篇:新手如何学好J2ME