📄 busquery.java
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class BusQuery implements Runnable {
private BQMIDlet midlet = null;
private String serverURL = null; //服务器定位符
private String userAgent = null; //客户端描述
private HttpConnection hc = null;
private byte[] queryCondition = new byte[0]; //查询条件
private ProcessUI progress;
private Thread queryThread;
private boolean isAlive = true;
private boolean forceStopQuery = false;
public BusQuery(BQMIDlet bqmidlet, String serverURL) {
this.midlet = bqmidlet;
this.serverURL = serverURL;
userAgent = "Profile:" + System.getProperty("microedition.profile") +
"\\Configuration:" + System.getProperty("microedition.configuration");
progress = new ProcessUI("网络查询");
progress.setCommandListener(new CommandListener() {
public void commandAction(Command cmd, Displayable displayable) {
stopQuery();
midlet.backToMain();
}
});
}
//初始化查询线程
public void init() {
if(queryThread == null || !queryThread.isAlive()) {
queryThread = new Thread(this);
queryThread.start();
}
}
//销毁查询线程
public void release() {
isAlive = false;
}
//停止查询
public void stopQuery() {
if(hc != null) {
synchronized(hc) {
try {
hc.close(); //关闭连接
forceStopQuery = true;
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
}
public void setCondition(int type, String condition) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(type);
dos.writeUTF(condition);
queryCondition = baos.toByteArray();
}
catch(IOException ioe) {
queryCondition = new byte[0];
ioe.printStackTrace();
}
}
public void execute() {
if(queryCondition.length > 0) {
synchronized(midlet) {
midlet.notify();
}
}
}
public void run() {
while(isAlive) {
synchronized(midlet) {
try {
midlet.wait();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
}
if(isAlive) {
try {
forceStopQuery = false;
progress.init();
midlet.setCurrent(progress);
progress.onProcess("正在连接服务器,发送查询请求...", 20);
//建立Http连接
hc = (HttpConnection)Connector.open(serverURL);
//设置请求方法\请求头属性
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", userAgent);
hc.setRequestProperty("Content-type", "application/octet-stream");
hc.setRequestProperty("Content-length", "" + queryCondition.length);
//获取与连接相关的输出流,写出查询条件
OutputStream os = hc.openOutputStream();
os.write(queryCondition);
os.flush();
//获得响应状态
int status = hc.getResponseCode();
if(status != HttpConnection.HTTP_OK) {
String msg = "网络连接错误!\n状态码:" + status + "\n描述:\n " + hc.getResponseMessage();
throw new IOException(msg);
}
//读取服务器的响应数据
DataInputStream dis = new DataInputStream(hc.openInputStream());
int size = dis.readInt();
Bus[] buses = new Bus[size];
String name = null;
String stations = null;
System.out.println("size = " + size);
for(int i=0; i<size; i++) {
name = dis.readUTF();
stations = dis.readUTF();
buses[i] = new Bus(name, stations);
progress.onProcess("读取查询结果...", 20+70/size*i);
}
if(size > 0) {
midlet.showBuses(buses);
}
else {
midlet.showError("没有要查询的公交线路!");
}
}
catch(IOException ioe) {
if(!forceStopQuery) {
midlet.showError(ioe.getMessage());
}
else {
midlet.showError("用户取消查询。");
}
}
finally {
try {
if(hc != null) {
hc.close();
}
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -