淮海工学院计算机工程学院
实验报告书
课程名: 《手持设备软件开发》
题 目: 实验4:J2ME网络程序设计
班 级:
学 号:
姓 名: statue
一、实验目的与要求
掌握MIDP网络程序设计的一般方法。能够使用GCF中的类和接口及多线程进行网络客户端应用程序的开发,掌握基于HTTP编程的方法,能够处理GET与POST请求。理解MIDlet推注册及响应网络连接的步骤及方法。
二、实验内容
使用Google Weather API开发一个可以显示连云港天气预报的J2ME网络程序。具体的方法是请求网址:le/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="le/ig/api?weather=lianyungang&hl=zh-cn";
public pro4()
{
Display(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( ionMatches( 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=")+"/><high data=".length()+1;
int high1StrEnd=str.indexOf("/><icon data=\"/ig")-1;
out=out+"最高温度:"+str.substring(high1StrStart,high1StrEnd);
int condition1StrStart=str.indexOf("gif\"/><condition data=")+"gif\"/><condition data=".length()+1;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论