/***********移动学生成绩查询系统**********
*
* 客户端程序
* 日期:2006-12-30
* 作者:张惠芳,陈锐华,张芊,欧军威
* 指导老师:仵博,池瑞楠
*
*****************************************/
// 导入包
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class ScoreQuery extends MIDlet implements CommandListener {
private Display display;
// 声明一个列表
private List mainList;
// 声明TFClass TFStuid用于客户端输入学号
private TextField TFStuid;
// 声明用于保存学号和学期的字符串
private String stuID, termName;
// 声明发送,返回,退出,确定按钮
private Command cmdSend, cmdBack, cmdExit, cmdOk;
// inputForm用于放置TFStuid等组件;returnForm用于放置查询结果
private Form inputForm, returnForm;
// 创建Alert放声明版权的图版和文字
private Alert anAlert;
// 声明一个Ticker
private Ticker aTicker;
// 声明一个图片
private Image anImage;
// 传送到ServletApp的服务器程序Servlet进行处理
final static String defaultURL = "http://localhost:81/examples/servlet/ServletApp";
// 线程运行条件
private boolean condition = false;
public ScoreQuery() {
// 版本声明的图片
try {
anImage = Image.createImage("/welcome.png");
} catch (IOException ioe) {
ioe.printStackTrace();
}
// 创建发送,返回,退出,确定按钮对象
cmdSend = new Command("发送",Command.EXIT,1);
cmdBack = new Command("返回",Command.EXIT,1);
cmdExit = new Command("退出",Command.EXIT,1);
cmdOk = new Command("确定",Command.EXIT,1);
aTicker = new Ticker("欢迎使用移动学生成绩查询系统");
anAlert = new Alert("版权声明!", " Copyright @ 2007 Shenzhen Polytechnic, All Rights Reserved.", anImage, AlertType.CONFIRMATION);
// 创建一个TEXTFIELD获取学生学号
TFStuid = new TextField("学号:", "3221101", 30, TextField.ANY);
// 输入学号的标题
inputForm = new Form("输入查找信息:");
// 输出查询结果的标题
returnForm = new Form("查询结果:");
inputForm.append(TFStuid);
// 创建退出和确定按钮
inputForm.addCommand(cmdExit);
inputForm.addCommand(cmdOk);
// 监听
inputForm.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
// 版本声明的Alert
anAlert.setTimeout(Alert.FOREVER);
display = Display.getDisplay(this);
display.setCurrent(anAlert, inputForm);
inputForm.setTicker(aTicker);
condition = false;
CommandThread commandThread = new CommandThread(this);
commandThread.start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
// 访问网络的方法
public void invokeServlet(String url) throws IOException {
// 创建输入输出流连接
HttpConnection conn = null;
DataOutputStream dos = null;
DataInputStream dis = null;
try {
// 用HttpConnection的openOutputStream()方法获取输出流,它将发送请求头到服务器端。
// 发出请求,打开网络
conn = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
// 设置请求方式为POST,默认的请求方式是GET
conn.setRequestMethod(HttpConnection.POST);
// 设置请求属性
conn.setRequestProperty("IF-Modified-Since", "15 Oct 2003 08:47:14 GMT");
conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-CA");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
/* Connection头可以控制MIDlet和Web服务器之间保持"keep alive"特色。
* "keep alive"特色是指在MIDlet和Web服务器间始终使用同一个HTTP连
* 接来多次传递数据。在通常情况下,HTTP是无连接的协议,每次数据传输完毕
* 后都将断开连接而下次传递数据之前将重新建立连接.
*/
conn.setRequestProperty("Connection", "Keep-Alive");
// 发送请求参数到servlet
dos = conn.openDataOutputStream();
// 发送请求参数给servlet
dos.writeUTF(termName);
dos.writeUTF(stuID);
// 关闭输入/输出流及其连接
dos.flush();
dos.close();
// 接收servlet响应数据
dis = new DataInputStream(conn.openInputStream());
// 返回
String returnScore = dis.readUTF();
System.out.print(returnScore);
// 将返回的结果append到resultForm中
returnForm = new Form("查询结果");
returnForm.append(returnScore);
// 创建返回按钮
returnForm.addCommand(cmdBack);
// 监听
returnForm.setCommandListener(this);
} catch (Exception e) {
} finally {
if (dis != null) {
// 关闭输入流
dis.close();
}
if (dos != null) {
// 关闭输出流
dos.close();
}
if (conn != null) {
// 关闭连接
conn.close();
}
}
// 显示查询结果
display.setCurrent(returnForm);
}
// CommandThread类用于处理访问网络时的多线程问题
class CommandThread extends Thread {
MIDlet parent;
boolean exit = false;
public CommandThread(MIDlet parent) {
this.parent = parent;
}
public void run() {
while(true) {
synchronized (parent) {
while (!condition) {
try {
parent.wait();
} catch (InterruptedException e) {
}
}
condition = false;
}
try {
invokeServlet(defaultURL);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
// 显示学期列表的方法
void ListScreen() {
// 列表的图片
Image[] imageArray = null;
try {
// 创建表示学期符号的图片
Image icon = Image.createImage("/symbol.png");
// 创建图片数组
imageArray = new Image[] {
icon,
icon,
icon,
icon,
icon
};
} catch (java.io.IOException err) {
System.out.print("load failed...");
imageArray=null;
}
// 列表数组
String[] stringArray = {"第一学期", "第二学期", "第三学期", "第四学期", "所有学期"};
mainList = new List("选择学期", Choice.IMPLICIT, stringArray, imageArray);
// 发送,返回按钮
mainList.addCommand(cmdBack);
mainList.addCommand(cmdSend);
// 监听
mainList.setCommandListener(this);
// 显示列表
display.setCurrent(mainList);
}
// 事件处理
public void commandAction(Command cmd,Displayable dis) {
// 返回
if (cmd == cmdBack) {
display.setCurrent(inputForm);
}
// 退出
else if (cmd == cmdExit) {
destroyApp(true);
notifyDestroyed();
}
// 确定
else if (cmd == cmdOk) {
ListScreen();
}
// 发送:当点击按钮Send时,获取输入在TexfField中学号
else if (cmd == cmdSend) {
if (dis.equals(mainList)) {
// 判断选择的列表
switch(((List)dis).getSelectedIndex()) {
case 0:
termName="第一学期";
break;
case 1:
termName="第二学期";
break;
case 2:
termName="第三学期";
break;
case 3:
termName="第四学期";
break;
case 4:
termName="所有学期";
break;
}
}
// 获取学号字符串
stuID = TFStuid.getString();
// 释放
synchronized (this) {
condition = true;
notify();
}
}
}
}
评论0