?? myconnection.java
字號:
/*
* 創建日期 2005-3-11
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.*;
/**
* @author huihui
*
* TODO 要更改此生成的類型注釋的模板,請轉至 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
public class MyConnection implements Runnable {
int result=-10;
boolean sendflag = false;
boolean getflag = false;
String request = null; //請求的URL
int next_wrong=0;
int next_right=0;
String content; //提示信息:“密碼驗證”、“選課”、“修改密碼”
MainMIDlet mainmidlet = null;
Thread thread = new Thread(this);
public MyConnection(MainMIDlet mainmidlet, String request, String content,int next_right,int next_wrong) {
this.mainmidlet = mainmidlet;
this.content = content;
this.request = request;
this.next_wrong = next_wrong;
this.next_right=next_right;
}
public void setSend() {
sendflag = true; //有信息待發送
}
public void setGet() {
getflag = true; //有信息待接收
}
public void ConnectToServer() throws IOException {
//byte[] buffer = null;
try {
//loading
this.mainmidlet.imagescreen.show(3,"連接服務器……",next_wrong); //3:load
HttpConnection connection = (HttpConnection) Connector.open(request, Connector.READ_WRITE, true);
if (sendflag) {
OutputStream os = null;
os = connection.openOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(this.mainmidlet.choosed); //選了幾門課
for (int i = 0; i < this.mainmidlet.choosed; i++) {
dos.writeInt(this.mainmidlet.courseChoosedid[i]);
}
dos.flush();
dos.close();
os.close();
}
InputStream is = null;
is = connection.openInputStream(); //打開輸入流
DataInputStream dis = new DataInputStream(is);
result = dis.readInt(); // -1代表用戶名不存在 -2代表密碼錯誤 -3代表與數據庫連接錯誤 (三個代表)
System.out.println(result);
if(result>1024){ //意外情況
result=0;
}
/*
if(result==-5){
int fullNum=dis.readInt();
System.out.println(fullNum);
for(int i=0;i<fullNum;i++)
System.out.println(dis.readInt());
}*/
if (getflag) {
//System.out.println("讀取課程!");
//帶緩沖讀取
/*
* int actual = 0; int bytesread = 0; buffer = new byte[len];
* while ((bytesread != len) && (actual != -1)) { actual =
* is.read(buffer, bytesread, len - bytesread); bytesread +=
* actual; //parse.parse(dis,dh); }
*/
if (result >= 0) { //>=0正常//
if (next_right == 2 ){ //選課列表
this.mainmidlet.courseNum = result;
result = 1;
this.mainmidlet.allcourseid = new int[this.mainmidlet.courseNum];
this.mainmidlet.allcourse = new String[this.mainmidlet.courseNum];
for (int i = 0; i < this.mainmidlet.courseNum; i++) {
this.mainmidlet.allcourseid[i] = dis.readInt();
this.mainmidlet.allcourse[i] = dis.readUTF();
//System.out.println(this.mainmidlet.allcourse[i]);
}
} else if (next_right==3) { //查詢
//System.out.println("result="+result);
this.mainmidlet.choosed = result;
this.mainmidlet.courseChoosed=new String[this.mainmidlet.choosed]; //申請字符串數組空間
result = 1;
for (int i = 0; i < this.mainmidlet.choosed; i++) {
this.mainmidlet.courseChoosedid[i] = dis.readInt();
this.mainmidlet.courseChoosed[i] = new String(dis.readUTF());
}
}
}
}
dis.close();
is.close();
connection.close();
//根據servelet返回信息判斷。 // -1代表用戶名不存在 -2代表密碼錯誤 -3代表與數據庫連接錯誤 -4代表更新數據庫錯誤
if (result == 1) {
if (next_right==2) {
this.mainmidlet.ShowChoose(); //直接進入選課界面
} else if (next_right == 3) {
this.mainmidlet.ShowQuery(); //直接進入查詢結果界面
} else {
//successful
this.mainmidlet.imagescreen.show(1,content + "成功",next_right);
this.mainmidlet.display.setCurrent(this.mainmidlet.imagescreen);
}
} else {
switch (result) { //判斷錯誤代碼
case -5:
content = "課程人數已滿";
break;
case -4:
content = "更新數據庫發生錯誤";
break;
case -3:
content = "與數據庫連接錯誤";
break;
case -2:
content = "密碼錯誤";
break;
case -1:
content = "用戶名不存在";
break;
default:
//System.out.println(content);
break;
}
//密碼錯誤需返回登錄界面
this.mainmidlet.imagescreen.show(2,"錯誤: " + content, next_wrong);
this.mainmidlet.display.setCurrent(this.mainmidlet.imagescreen);
}
} catch (IOException e) {
e.printStackTrace();
//connection failed
this.mainmidlet.imagescreen.show(2,"連接服務器失敗", next_wrong);
//應返回登錄框。
}
}
public void start() {
try {
thread.start();
} catch (Exception error) {
error.printStackTrace();
}
}
public void run() {
try {
ConnectToServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -