WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术开发环境在J2ME平台上构建你的邮件程序

在J2ME平台上构建你的邮件程序
作者:Jacky Pan  来源:转载  发布时间:2005-8-2 9:56:48


}

catch(MessagingException me)

{

out.println("<error>" + me.toString() + "</error>");

}



out.println("</mail>");

}



在MIDlet端再解析这个XML,在J2ME平台上有许多免费的XML Parser,kxml就是其中的一个。可以从 http://kxml.enhydra.org/ 下载kXML 1.21的源代码,jar文件以及API文档。



下面是utility/HeadParser.java中处理XML的代码片断

public void parse(InputStream in) throws IOException

{

Reader reader = new InputStreamReader(in);

XmlParser parser = new XmlParser(reader);

ParseEvent pe = null;



parser.skip();

/* 读一个名字为mail的event */

parser.read(Xml.START_TAG, null, "mail");



boolean trucking = true;

boolean first = true;



while (trucking)

{

/* 读取下一个event */

pe = parser.read();



if (pe.getType() == Xml.START_TAG)

{

/* 得到event的名字 */

String name = pe.getName();


if (name.equals("message"))

{

String from = null;

String subject = null;

String date = null;

int index = -1;



while ((pe.getType() != Xml.END_TAG) ||

(pe.getName().equals(name) == false))

{

pe = parser.read();

if (pe.getType() == Xml.START_TAG &&

pe.getName().equals("subject"))

{

pe = parser.read();

/* 得到event的内容 */

subject = pe.getText();

}

else if (pe.getType() == Xml.START_TAG &&

pe.getName().equals("from"))

{

pe = parser.read();

from = pe.getText();

}

else if (pe.getType() == Xml.START_TAG &&

pe.getName().equals("date"))

{

pe = parser.read();

date = pe.getText();

}

else if (pe.getType() == Xml.START_TAG &&

pe.getName().equals("index"))

{

pe = parser.read();

index = Integer.parseInt(pe.getText());

}



}



/* 把邮件头交给监听器处理 */

headListener.itemParsed(from, subject, date, index);

}

else //Non Message block

{

while ((pe.getType() != Xml.END_TAG) ||

(pe.getName().equals(name) == false))

pe = parser.read();

}

}

if (pe.getType() == Xml.END_TAG &&

pe.getName().equals("mail"))

{

trucking = false;

}



}

具体的API的用法请大家参照kxml的文档。JSR172 (J2ME Web Services Specification)提出了在J2ME平台上处理XML的规范,有兴趣的朋友可以到jcp的网站 (http://www.jcp.org) 上看看,但目前可能还没有厂商或组织的实现。

8. 小结

本文介绍了J2ME平台上邮件程序的编写,涉及的知识点有:

1. J2ME的UI

2. Record Store

3. J2ME的网络连接 / J2ME 和J2EE之间数据的传递

4. Parsing XML in J2ME

5. 简单的Servlet

6. Java Mail APIs

参考资料有:

1. MIDP2.0 Spec, http://www.jcp.org

2. <<Core J2ME>>, http://www.corej2me.com/

3. <<J2ME in Nutshell>>, http://www.oreilly.com

4. Tutorial for building J2EE Applications using JBOSS and ECLIPSE, http://www.tusc.com.au/tutorial/html/

上一页  [1] [2] [3] 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码