📄 httpconnection.java
字号:
/*
* HTTPConnection.java
*
* Created on 2006年8月22日, 下午1:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cn.edu.nwpu.MobileCampusClinet;
import cn.edu.nwpu.MobileCampusServer.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;
/**
*
* @author SliverYinG
*/
public class HTTPConnection {
//服务器的URL
private String serviceURL="http://localhost:8084/MobileCampusServer/show";
//是否在线
private boolean inline=false;
private GaugeObserverUI gaugeObserverUI=null;
/** Creates a new instance of HTTPConnection */
public HTTPConnection(GaugeObserverUI gaugeObserverUI){
this.gaugeObserverUI=gaugeObserverUI;
}
//判断是否在联网
public boolean isInline(){
return inline;
}
public void setInline(boolean inline){
this.inline=inline;
}
//处理图书馆模块
public Vector getBook(String bookName){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.SEARCH_BOOK);
dos.writeUTF(bookName);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
int size =dis.readInt();
Vector storeBook=new Vector();
for(int i =0 ; i<size ; i++){
BookBean book=BookBean.deserialize(dis);
storeBook.addElement(book);
}
updateGauge();
return storeBook;
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
return null;
}
public Vector getElective(){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.SEARCH_ELECTIVE);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
int size =dis.readInt();
Vector storeElective=new Vector();
for(int i =0 ; i<size ; i++){
ElectiveBean elective=ElectiveBean.deserialize(dis);
storeElective.addElement(elective);
}
updateGauge();
return storeElective;
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
return null;
}
//处理词典模块
public Vector getMeaning(String word){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.ENGLISH_CHANGE_CHINESE);
dos.writeUTF(word);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
int size =dis.readInt();
Vector storeMeaning=new Vector();
for(int i =0 ; i<size ; i++){
DictionaryBean meaning=DictionaryBean.deserialize(dis);
storeMeaning.addElement(meaning);
}
updateGauge();
return storeMeaning;
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
return null;
}
//处理个人信息中的选修课模块
public Vector getChoiceElective(UserBean user){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.LOOK_CHIOCE_ELECTIVE);
user.serialize(dos);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
int size =dis.readInt();
Vector storeChoiceElective=new Vector();
for(int i =0 ; i<size ; i++){
ChoiceBean choiceElective=ChoiceBean.deserialize(dis);
storeChoiceElective.addElement(choiceElective);
}
updateGauge();
return storeChoiceElective;
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
return null;
}
//处理个人信息中的已借图书模块
public Vector getBorrowBook(UserBean user){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.HAS_BORROW_BOOK);
user.serialize(dos);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
int size =dis.readInt();
Vector storeBorrowBook=new Vector();
for(int i =0 ; i<size ; i++){
BorrowBean borrowBook=BorrowBean.deserialize(dis);
storeBorrowBook.addElement(borrowBook);
}
updateGauge();
return storeBorrowBook;
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
return null;
}
//处理添加选修课模块
public boolean addElective(String userName , String electiveName){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
byte addSuccess=0;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.ADD_ELECTIVE);
dos.writeUTF(userName);
dos.writeUTF(electiveName);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
addSuccess=dis.readByte();
updateGauge();
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
if(addSuccess==OperateConstants.ADD_ELECTIVE_SUCCESS){
return true;
}else{
return false;
}
}
//处理个人信息中的修改密码模块
public boolean changPassWord(UserBean user , String newPassword){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
byte success=0;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.CHANGE_PWD);
user.serialize(dos);
dos.writeUTF(newPassword);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
success=dis.readByte();
updateGauge();
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
if(success==OperateConstants.CHANGEPWD_SUCCESS){
return true;
}else{
return false;
}
}
//处理登陆模块
public boolean getLogin(String userName , String passWord){
HttpConnection Httpconn=null;
DataOutputStream dos=null;
DataInputStream dis=null;
byte pass=0;
try{
Httpconn=openConnection();
updateGauge();
dos=openConnectionDOS(Httpconn);
dos.writeByte(OperateConstants.LOGIN);
dos.writeUTF(userName);
dos.writeUTF(passWord);
dos.close();
updateGauge();
dis=openConnectionDIS(Httpconn);
pass=dis.readByte();
updateGauge();
}catch(Exception e){
System.out.println(e.toString());
}finally{
if(gaugeObserverUI!=null){
gaugeObserverUI=null;
}
closeConnection(Httpconn,dos,dis);
}
if(pass==OperateConstants.LOGIN_SUCCESS){
return true;
}else{
return false;
}
}
//打开网络连接
private HttpConnection openConnection() throws IOException{
try{
HttpConnection conn=(HttpConnection) Connector.open(serviceURL);
conn.setRequestProperty("Content-Type","application/octet-stream");
conn.setRequestMethod(HttpConnection.POST);
inline=true;
return conn;
}catch(IOException e){
inline=false;
throw e;
}
}
//打开数据输出流
private DataOutputStream openConnectionDOS(HttpConnection conn)throws IOException{
try{
return conn.openDataOutputStream();
}catch(IOException e){
inline=false;
throw e;
}
}
//打开数据输入流
private DataInputStream openConnectionDIS(HttpConnection conn)throws IOException{
try{
return conn.openDataInputStream();
}catch(IOException e){
inline=false;
}
return null;
}
//关闭所有连接
private void closeConnection(HttpConnection httpconn,DataOutputStream dos,DataInputStream dis){
if(dos!=null){
try{
dos.close();
}catch(IOException e){
}
}
if(dis!=null){
try{
dis.close();
}catch(IOException e){
}
}
if(httpconn!=null){
try{
httpconn.close();
}catch(IOException e){
}
}
}
//更新进度条
protected void updateGauge(){
if(gaugeObserverUI!=null){
if(!gaugeObserverUI.isStopped()){
gaugeObserverUI.updateGauge();
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -