跟我学J2ME手机应用开发——MidLet编程技术及应用实例
- 格式:pdf
- 大小:368.88 KB
- 文档页数:10
淮海工学院计算机工程学院实验报告书课程名:《手持设备软件开发》题目:实验4:J2ME网络程序设计班级:学号:姓名:statue评语:成绩:指导教师:批阅时间:年月日一、实验目的与要求掌握MIDP网络程序设计的一般方法。
能够使用GCF中的类和接口及多线程进行网络客户端应用程序的开发,掌握基于HTTP编程的方法,能够处理GET与POST请求。
理解MIDlet推注册及响应网络连接的步骤及方法。
二、实验内容使用Google Weather API开发一个可以显示连云港天气预报的J2ME网络程序。
具体的方法是请求网址:/ig/api?weather=lianyungang&hl=zh-cn然后处理响应返回的XML文档。
样例文档如下:<xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"row="0" section="0"><forecast_information><city data="Lianyungang, Jiangsu" /><postal_code data="lianyungang" /><latitude_e6 data="" /><longitude_e6 data="" /><forecast_date data="2010-06-03" /><current_date_time data="2010-06-03 11:00:00 +0000" /><unit_system data="SI" /></forecast_information><current_conditions><condition data="雨" /><temp_f data="58" /><temp_c data="14" /><humidity data="湿度:89%" /><icon data="/ig/images/weather/cn_heavyrain.gif" /><wind_condition data="风向:米/秒" /></current_conditions><forecast_conditions><day_of_week data="周五" /><low data="14" /><high data="23" /><icon data="/ig/images/weather/sunny.gif" /><condition data="晴" /></forecast_conditions>……</weather></xml_api_reply>三、实验步骤import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.*;import java.util.Vector;public class pro4 extends MIDlet implements CommandListener{public static final String RESPONSE_HEADER_CONTENT_LENGTH = "Content-Length";public static final String RESPONSE_HEADER_CONTENT_TYPE = "Content-Type";private Display display;private TextBox tbxMain;private Command cmdExit;private Command cmdDisplay;private String msg="";String url="/ig/api?weather=lianyungang&hl=zh-cn";public pro4(){display=Display.getDisplay(this);cmdExit=new Command("Exit",Command.SCREEN,1);cmdDisplay=new Command("刷新",Command.ITEM,2);getWeather();tbxMain=new TextBox("天气预报",msg,2000,0);tbxMain.addCommand(cmdExit);tbxMain.addCommand(cmdDisplay);tbxMain.setCommandListener(this);}//建立连接public void getWeather(){HttpConnection conn=null;try{conn=(HttpConnection)Connector.open(url,Connector.READ);conn.setRequestMethod(HttpConnection.POST);conn.setRequestProperty("User_agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");}catch(IOException ioe){System.out.println(ioe);}try{DataInputStream sIs = conn.openDataInputStream();String outString=getResponseData(conn,sIs);System.out.println(outString);msg=outString;msg=division(msg);}catch(IOException io){System.out.print(io);}}//分割提取字符串public String division(String str){String out="";int cityStrStart=str.indexOf("city data=")+"city data=".length()+1;int cityStrEnd=str.indexOf("/><postal_code")-1;out=out+"城市:"+str.substring(cityStrStart,cityStrEnd)+'\n';int dateStrStart=str.indexOf("orecast_date data=")+"orecast_date data=".length()+1;int dateStrEnd=str.indexOf("/><current_date")-1;out=out+"日期:"+str.substring(dateStrStart,dateStrEnd)+'\n';int wetStrStart=str.indexOf("湿度:")+"湿度:".length();int wetStrEnd=str.indexOf("/><icon data=")-1;out=out+"湿度:"+str.substring(wetStrStart,wetStrEnd)+'\n';if( str.regionMatches( true,str.indexOf("gif\"/><wind_condition data=")+1 ,"风向" ,0 ,"风向".length() ) ){int windStrStart=str.indexOf("风向:")+"风向:".length();int windStrEnd=str.indexOf("/></current_conditions>")-1;out=out+"风向:"+str.substring(windStrStart,windStrEnd)+'\n';}//第一天int week1StrStart=str.indexOf("day_of_week data=")+"day_of_week data=".length()+1;int week1StrEnd=str.indexOf("/><low data=")-1;out=out+str.substring(week1StrStart,week1StrEnd)+'\n';int low1StrStart=str.indexOf("low data=")+"low data=".length()+1;int low1StrEnd=str.indexOf("/><high data=")-1;out=out+"最低温度:"+str.substring(low1StrStart,low1StrEnd);int high1StrStart=str.indexOf("/><high data=")+"/><highdata=".length()+1;int high1StrEnd=str.indexOf("/><icon data=\"/ig")-1;out=out+"最高温度:"+str.substring(high1StrStart,high1StrEnd);int condition1StrStart=str.indexOf("gif\"/><conditiondata=")+"gif\"/><condition data=".length()+1;int condition1StrEnd=str.indexOf("/></forecast_conditions")-1;out=out+"总体概况:"+str.substring(condition1StrStart,condition1StrEnd)+'\n';//第二天int week2StrStart=str.indexOf("day_of_weekdata=",condition1StrEnd+5)+"day_of_week data=".length()+1;int week2StrEnd=str.indexOf("/><low data=",condition1StrEnd+5)-1; out=out+str.substring(week2StrStart,week2StrEnd)+'\n';int low2StrStart=str.indexOf("low data=",condition1StrEnd+5)+"low data=".length()+1;int low2StrEnd=str.indexOf("/><high data=",condition1StrEnd+5)-1;out=out+"最低温度:"+str.substring(low2StrStart,low2StrEnd);int high2StrStart=str.indexOf("/><highdata=",condition1StrEnd+5)+"/><high data=".length()+1;int high2StrEnd=str.indexOf("/><icon data=\"/ig",condition1StrEnd+5)-1;out=out+"最高温度:"+str.substring(high2StrStart,high2StrEnd);int condition2StrStart=str.indexOf("gif\"/><conditiondata=",condition1StrEnd+5)+"gif\"/><condition data=".length()+1;intcondition2StrEnd=str.indexOf("/></forecast_conditions",condition1StrEnd+5)-1;out=out+"总体概况:"+str.substring(condition2StrStart,condition2StrEnd)+'\n';//第三天int week3StrStart=str.indexOf("day_of_weekdata=",condition2StrEnd+5)+"day_of_week data=".length()+1;int week3StrEnd=str.indexOf("/><low data=",condition2StrEnd+5)-1;out=out+str.substring(week3StrStart,week3StrEnd)+'\n';int low3StrStart=str.indexOf("low data=",condition2StrEnd+5)+"lowdata=".length()+1;int low3StrEnd=str.indexOf("/><high data=",condition2StrEnd+5)-1;out=out+"最低温度:"+str.substring(low3StrStart,low3StrEnd);int high3StrStart=str.indexOf("/><highdata=",condition2StrEnd+5)+"/><high data=".length()+1;int high3StrEnd=str.indexOf("/><icon data=\"/ig",condition2StrEnd+5)-1;out=out+"最高温度:"+str.substring(high3StrStart,high3StrEnd);int condition3StrStart=str.indexOf("gif\"/><conditiondata=",condition2StrEnd+5)+"gif\"/><condition data=".length()+1;intcondition3StrEnd=str.indexOf("/></forecast_conditions",condition2StrEnd+5)-1;out=out+"总体概况:"+str.substring(condition3StrStart,condition3StrEnd)+'\n';//第四天int week4StrStart=str.indexOf("day_of_weekdata=",condition3StrEnd+5)+"day_of_week data=".length()+1;int week4StrEnd=str.indexOf("/><low data=",condition3StrEnd+5)-1;out=out+str.substring(week4StrStart,week4StrEnd)+'\n';int low4StrStart=str.indexOf("low data=",condition3StrEnd+5)+"lowdata=".length()+1;int low4StrEnd=str.indexOf("/><high data=",condition3StrEnd+5)-1;out=out+"最低温度:"+str.substring(low4StrStart,low4StrEnd);int high4StrStart=str.indexOf("/><highdata=",condition3StrEnd+5)+"/><high data=".length()+1;int high4StrEnd=str.indexOf("/><icon data=\"/ig",condition3StrEnd+5)-1;out=out+"最高温度:"+str.substring(high4StrStart,high4StrEnd);int condition4StrStart=str.indexOf("gif\"/><conditiondata=",condition3StrEnd+5)+"gif\"/><condition data=".length()+1;intcondition4StrEnd=str.indexOf("/></forecast_conditions",condition3StrEnd+5)-1;out=out+"总体概况:"+str.substring(condition4StrStart,condition4StrEnd)+'\n';return out;}//获取内容public String getResponseData(HttpConnection aConn, DataInputStream sIs) { boolean isWap = true;byte[] sData = null;String sType = null;int sLen = 0;try {sType = aConn.getHeaderField(RESPONSE_HEADER_CONTENT_TYPE);sLen = Integer.parseInt(getResponseHeaderProperty(aConn, RESPONSE_HEADER_CONTENT_LENGTH));} catch (IOException ex) {System.err.print(ex.toString());} catch (Exception ex) {System.err.print(ex.toString());sLen = 0;}if (sLen > 0) {int sActual = 0; //每一次实际读取的字节数int sBytesread = 0; //总读取的字节数sData = new byte[sLen];while ((sBytesread != sLen) && (sActual != -1)) {try {sActual = sIs.read(sData, sBytesread, sLen - sBytesread); sBytesread += sActual;} catch (Exception ex) {System.err.print(ex.toString());return null;}}} else {int sCh = 0;Vector sVbuffer = new Vector();try {while ((sCh = sIs.read()) != -1) {sVbuffer.addElement(new Integer(sCh));}} catch (Exception ex) {System.err.print(ex.toString());return null;}sLen = sVbuffer.size();sData = new byte[sLen];for (int i = 0; i < sLen; i++) {sData[i] = ((Integer) sVbuffer.elementAt(i)).byteValue();}}String sResult = new String(sData);isWap = false;return sResult;}public String getResponseHeaderProperty(HttpConnection aConn, String aKey) {return aConn.getRequestProperty(aKey);}public void startApp(){display.setCurrent(tbxMain);}public void pauseApp(){}public void destroyApp(boolean unconditional){}public void commandAction(Command c,Displayable s){if(c==cmdExit){destroyApp(false);notifyDestroyed();}if(c==cmdDisplay){getWeather();tbxMain.setString(msg);System.out.println("msg"+msg);}}}四、实验结果五、结果分析与实验体会本次实验可以分为三个部分:建立连接,取出字符串,提取字符串。
《J2ME程序设计》课程教学大纲一、课程基本情况开课单位:计算机与信息工程系课程编码:适用专业:软件修课方式:必须总学时:68学时考核方式:考试教材:《J2ME移动应用开发》郝玉龙北方交通大学出版社 2006.9教学参考书:《精通J2ME无线编程》,林胜利,中国铁道出版社,2004年二、课程的性质、任务和目的本课程是计算机软件专业的一门专业基础课程,重在培养学生的移动设备实践开发能力,适应软件企业的工作环境和业界标准,并和国际先进的软件开发理念保持同步。
通过本课程学习,学生应该了解并掌握J2ME技术基础,CLDC与MIDP基本概念,熟练应用J2ME技术开发基于移动设备的应用系统或游戏系统,并了解移动增值业务系统的开发基础。
学习本课程学生应达到如下要求或能力:1、掌握J2ME技术基础以及CLDC与MIDP的基本概念2、熟练应用J2ME技术编写简单应用软件系统3、熟练应用J2ME技术编写简单游戏4、了解无线增值业务(如SMS)应用系统的开发三、课程的主要内容和学时分配(一) 主要内容第1章 J2ME概述 2学时1.1 J2ME简介1.2 J2ME架构1.3 技术规范第2章 Hello World 2+2学时2.1 第一个实例2.2 J2ME程序开发流程2.3 代码解释2.4 JBuilder 9的配置2.5 使用JBuilder 9开发Hello World第3章 MIDlet和MIDlet Suite 4+2学时3.1 MIDlet概述3.2 MIDlet的打包3.3 MIDlet的生命周期3.4 MIDlet的安全管理第4章 MIDP用户界面——高级API 6+2学时4.1 MIDP用户界面概述4.2 高级API详述4.3 开发实用程序第5章 MIDP用户界面库——底层API 8+4学时5.1 Canvas类概述5.2 设备及屏幕属性5.3 基本作图属性设置5.4 基本图形操作5.5 剪切区操作5.6 输入处理5.7 多线程5.8 定制Item5.9 开发实用程序第6章 MIDP游戏编程 6+4学时 6.1 游戏API概述6.2 GameCanvas类6.3 LayerManager和Layer6.4 Sprite6.5 TiledLayer6.6 游戏设计第7章 MIDP网络编程 8+4学时 7.1 通用连接框架7.2 输入输出流7.3 HTTP协议7.4 使用HttpConnection编程7.5 Socket编程7.6 无线消息传递7.7 PushRegistry7.8 网络安全和MIDlet的签名认证第8章永久存储 4+2学时 8.1 永久存储概述8.2 RMS基础8.3 账务管理程序附录A J2ME开发基本环境设置 2+2学时 A.1 获取软件包A.2 软件包安装与配置A.3 J2ME网络资源附录B CLDC概述 2+2学时 B.1 CLDC的目标和功能B.2 CLDC 1.1与CLDC 1.0的区别B.3 CLDC的安全要求B.4 CLDC中虚拟机的变化B.5 CLDC中的类库附录C MIDP概述 2+2学时 C.1 MIDP的范围和软硬件要求C.2 MIDP的包概述C.3 MIDP的部署-Over-The-Air (OTA)C.4 MIDP程序的效率Borland认证课程介绍(二) 学时分配四、课程教学基本要求及重点1.J2ME概述了解J2ME的基本概念,开发架构以及采用的规范,掌握J2ME开发所需基本条件以及开发环境所需软件基础。
基于J2ME的手机游戏开发实例与发展分析摘要:随着java技术的不断更新,j2me技术在手机游戏的应用逐渐成为游戏市场的新亮点。
文章首先介绍了j2me技术的原理和架构,之后通过实例分析了j2me在手机游戏中的应用,最后分析了j2me手机游戏的开发意义。
关键词:j2me;midp;软件应用;软件开发中图分类号:tp311.52 文献标识码:a 文章编号:1007-9599 (2012) 24-0188-02随着java语言的不断发展,已经逐渐成长为一种尖端的程序设计语言。
这个最初用来编写与硬件不相关的嵌入式系统的编程语言,现在更是做到了与厂商不相关、与硬件不相关的服务器端技术,在软件开发应用中展示出了强大的生命力和发展前景。
j2me技术正是基于java技术规范的,高度优化的java运行环境,在消费电子领域占据着强有力的位置,尤其是手机游戏的开发上,更是发挥的淋漓尽致。
当前j2me技术最流行的应用就是在制作手机游戏方面,java手机的出现更为这种技术的发挥提供了宽阔的平台。
1 j2me技术及其架构java2 micro edition (j2me)是java2技术微型版,与j2se、j2ee同样具有美国sun微系统公司提出的java技术规范,主要针对消费电子市场,为机顶盒、移动电话和pda之类嵌入式消费电子设备提供的java语言平台。
j2me的运行环境与目标更加多样化,而每一个产品的用途却变得更加单一,资源限制也更为严格,其构成主要是由概要描述、配置、java虚拟机和操作系统等部分组成的标准架构之上的。
这种架构下模块化设计很好的适应了小型计算设备的应用限制,通过j2me架构甚至可以替代小型计算设备的操作系统。
j2me的架构分为configuration、profile和optional packages,它们的组合取舍形成了具体的运行环境。
第一层configuration的作用是与原计算机设备所带有的系统进行互换,它里面还存在着一个配置层,负责处理profile和jvm之间的交互;第二层的profile层,主要是设备的应用程序编程用的接口集合,它对设备横向分类,针对特定领域细分市场,内容主要包括特定用途的类库和api;第三层的optional packages是独立于前面两者提供附加的、模块化的和更为多样化的功能,比如多媒体应用、蓝牙等。
J2ME MIDP v2.0 开发综合实例作者:闻怡洋 2004/02/08 /版权说明:本文版权属于作者闻怡洋,在未经过作者允许的情况下禁止以下行为:●在互联网上转载本文。
●在任何盈利性杂志或是盈利性行为中使用本文。
关键字:J2ME MIDP 实例开发编程 MIDlet WTK综合实例文章目录第 1 章预备知识21.1《J2ME MIDP 1.0/2.0 无线设备编程指南》参考书介绍21.2开发工具介绍21.3SunONE Studio 4 ME 的配置2第 2 章综合实例介绍32.1实例讲解思路32.2功能介绍32.3界面介绍3第 3 章程序设计53.1界面间的转换53.2类静态图53.3程序启动序列图73.4通过 WEB 下载产品清单序列图7第 4 章程序实现74.1准备工作84.2AboutBox 类的实现104.3ProductDb 类的实现114.4ProductLoader 类的实现144.5HttpLoadForm 类的实现174.6ListForm 类的实现204.7AntiJPMainForm 类的实现224.8AntiJP 类的实现244.9程序的执行26第 5 章结束语27第 1 章预备知识1.1《J2ME MIDP 1.0/2.0 无线设备编程指南》参考书介绍我编写了一本名为《J2ME MIDP 1.0/2.0 无线设备编程指南》的参考书(此参考书即将由北大出版社出版)。
参考书的内容集中在利用MIDP在CLDC设备上进行开发方面,内容覆盖了开发环境的建立、界面开发、多媒体播放、数据库存储、网络开发。
参考书的内容包括了:●WTK 开发包的使用,也包括MIDP 开发的其他工具和其他手机厂商提供的开发包介绍。
●程序基本开发知识,介绍MIDlet 程序的生存周期,如何使用菜单命令。
●低级界面开发与作图知识,MIDP v2.0 对此部分有较大改进。
●高级界面开发知识,MIDP v2.0 对此部分有较大改进。
J2ME手机游戏设计之“打砖块”一、游戏分析:随着现代社会发展的日新月异,人们平常工作学习的竞争也在加大,生活的压力也跟着加重。
所以利用闲暇之余,大家都需要休闲娱乐工具,来放松自己,缓解压力,消除紧张的疲劳状态。
在这样的背景下,打砖块游戏作为一向休闲手机游戏应运而生,它不仅能舒缓我们的身心,而且能够开发我们的动手能力,提高自己的反应敏捷程度,充实自己。
二、游戏规则设计:BeatBrick:打砖块游戏的游戏规则为,每局有三个球,也就是有三条生命。
首先发球,球在击打砖块后弹回。
此时,须用球拍接住弹回的球并令其继续击打砖块。
否则,球会掉出屏幕。
直至球全部使用完或通全关则游戏结束。
游戏开始时,小球会停在挡板正中间,且此时挡板可左右自由移动,当按下空格键后小球会弹出,并在程序设定的区域内不停的碰撞反弹。
当小球碰到墙壁、挡板和砖块时,均会以相反的速度反弹,并且砖块被碰撞后会自动消失。
挡板可左右移动以接住小球,若小球没有被接住,超出指定区域下边缘,则程序会提示游戏结束,点击“确定”后退出程序。
当所有的砖块均消失时,程序会提示你赢了。
三、游戏实现:1、J2ME,即Java 2 Micro Edition,是SUN公司推出的在移动设备上运行的微型版Java平台,常见的移动设备有手机,PDA,电子词典,以及各式各样的信息终端如机顶盒等等。
最重要的移动终端当然是手机了,因此,我们主要讨论手机相关的J2ME规范。
J2ME是Sun公司为嵌入式开发所定义的一个框架,一系列标准的规范所组成。
所以J2ME是一个协议簇,而不是单一的规范。
2、游戏由6个类构成:GameScreen:GameScreen类表示游戏的核心,它包括对象的移动、图像的绘制以读取和响应用户的输人。
本游戏从MIDP LCDUI的低级Canvas类派生一个类来代表游戏的屏幕,后面将对这个类进行详细的说明。
BeatBrick:由MIDIet继承而来的应用程序类来代表这个应用程序。