📄 motoshop.java.txt
字号:
import java.util.*;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class MotoShop extends MIDlet implements CommandListener {
//Main显示
private Display mainDisplay;
private Command MotoShop_HOME = new Command ("MAIN",Command.OK,1);
//keywordScree和isbnScree上使用的SUBMIT按钮
private Command MotoShop_SUBMIT = new Command("SUBMIT",Command.OK,1);
//用于保存搜索结果的Vector
private Vector bookList;
private Book currBook;
//mainMenu屏及其命令
List mainMenu;
private Command mm_GO = new Command("EXIT",Command.OK,1);
//keywordScreen
Form keywordScreen;
TextField keywordField;
//isbnScreen
Form isbnScreen;
TextField isbnField;
//resultScreen及其命令
List resultScreen;
private Command rs_SELECT = new Command("SELECT",Command.OK,1);
public MotoShop(){
initMainMenuScreen();
mainDisplay = Display.getDisplay(this);
}
public void startApp()
throws MIDletStateChangeException{
mainDisplay.setCurrent(mainMenu);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
//初始化"mainMenu"屏
void initMainMenuScreen(){
mainMenu = new List("MotoShop:Books",List.IMPLICIT);
mainMenu.append(">> Search by Keywords",null);
mainMenu.append(">> Search by ISBN",null);
mainMenu.setCommandListener(this);
mainMenu.addCommand(mm_GO);
mainMenu.addCommand(mm_EXIT);
}
//初始化"keywordScreen"屏
void initKeywordScreen(){
keywordScreen = new Form("MotoShop:Books");
keywordField = new TextField(
"Enter keyword(s): ","java xml",20,TextField.ANY);
keywordScreen.append(keywordField);
keywordScreen.append("Example:java xml");
keywordScreen.setCommandListener(this);
keywordScreen.addCommand(MotoShop_SUBMIT);
keywordScreen.addCommand(MotoShop_HOME);
}
//初始化"isbnScreen"屏
void initIsbnScreen(){
isbnScreen = new Form("MotoShop:Books");
isbnField = new TextField(
"Enter ISBN#: ","1565924878",10,TextField.NUMERIC);
isbnScreen.append(isbnField);
isbnScreen.append("Example:1565924878");
isbnScreen.setCommandListener(this);
isbnScreen.addCommand(MotoShop_SUBMIT);
isbnScreen.addCommand(MotoShop_HOME);
}
//初始化"resultScreen"屏
void initResultScreen(Vector _bookList){
resultScreen = new List("Search Results: ",List.IMPLICIT);
Enumeration e = _bookList.elements();
while(e.hasNireElements())
resultScreen.append(
">"+((Book)e.nextElement()).getTitle(),null);
resultScreen.setCommandListener(this);
resultScreen.addCommand(rs_SELECT);
resultScreen.addCommand(MotoShop_HOME);
}
//初始化"detailScreen"屏
void initDetailScreen(Book _currBook){
detailScreen = new Form("Detail Info:");
detailScreen.append(
"Title: "+_currBook.getTitle() + "/n");
detailScreen.append(
"Author: "+_currBook.getAuthor() + "/n");
detailScreen.append(
"Publisher: "+_currBook.getPublisher() + "/n");
Vector priceList = _currBook.getPrices();
for (int i = 0;i < priceList.size();i++){
Pair price = (Pair)priceList.elementAt(i);
detailScreen.append(">"+priceList.getSiteName()+"/n");
detailScreen.append(">"+priceList.getPrice()+"/n");
}
detailScreen.addCommand(MotoShop_HOME);
detailScreen.addCommand(ds_BACK);
detailScreen.setCommandListener(this);
}
//命令操作处理
public void commandAction(
Command command,Displayable display){
Screen screen = (Screen) display;
if(command == MotoShop_HOME){
mainDisplay.setCurrent(mainMenu);
}else if( Command == mm_GO ){
//选择了keywordScreen
if(mainMenu.getSelectedIndex() == 0 ){
initKeywordScreen();
mainDisplay.setCurrent(keywordScreen);
//选择了isbnScreen
}else if (mainMenu.getSelectedIndex() == 1){
initIsbnScreen();
mainDisplay.setCurrent(isbnScreen);
}
//中止MotoShop MIDlet
}else if ( command == mm_EXIT ){
destroyApp(false);
notifyDestroyed();
}
}else if ( screen == keywordScreen){
if ( command == MotoShop_SUBMIT){
String requestString = kerwordField.getString();
if( !requestString.equals("")){
String queryString = "keyword=yes";
try{
//向服务器发送关键字搜索请求
bookList = searchBooks(queryString,requestString);
//显示搜索结果
initResultScreen(bookList);
}catch (IOException ioe){
System.out.println("IO Error:"+ioe.toString());
}
}
}
}else if (screen == isbnScreen){
if (command == MotoShop_SUBMIT){
String requestString = isbnField.getString();
if (!requestString.equals("")){
String quertString = "isbn=yes";
try{
//向服务器发送isbn搜索请求
bookList = searchBooks(queryString,requestString);
//显示搜索结果
initResultScreen(bookList);
mainDisplay.setCurrent(resultScreen);
}catch (IOException ioe){
System.out.println("IO Error:"+ioe.toString());
}
}
}
}else if(screen == resultScreen){
//选择了一本书
if(command == rs_SELECT){
int index = resultScreen.getSelectIndex();
if(index != —1){
currBook = (Book) bookList.elementAt(index);
String requestString = currBook.getIsbn();
//如果此书无价格信息
//则用book39s ISBN号检索
//所用图书的价格信息
if(currBook.getPrides().size() == 0){
String queryString = "isbn=yes";
try{
Vector tmpBookList = searchBooks(queryString,requestString);
if(tmpBookList.size() > 0)
currBook = (Book) tmpBookList.elementAt(0);
bookList.setElementAt(currBook,index);
}catch (IOException ioe){
System.out.println("IO Error:"+ioe.toString());
}
}
//显示图书详细信息
initDetailScreen(currBook);
mainDisplay.setCurrent(detailScreen);
}else {
mainDisplay.setCurrent(mainMenu);
}
}
}else if(screen == detailScreen){
if(command == ds_BACK){
mainDisplay.setCurrent(resultScreen);
}
}
private Vector searchBooks(
String queryString,String searchString) throws IOException (
//MotoShop39s搜索服务器的基本URL
String baseURL = "http://www.webyu.com/servlets/webyu/MotoShopServer?";
//用于保存搜索结果的Vector
Vector bookList = null;
//用于与服务器通信的I/O流
HttpConnection hc = null;
InputStream is = null;
DataOutputStream os = null;
try{
//向baseURL追加queryString
String urlString = baseURL + queryString;
//需要读、写访问权限
hc = (HttpConnection)Connection.open(urlString,Connector.READ_WRITE);
//将请求方法置为POST
hc.setRequestMethod(HttpConnection.POST);
//用outputstream os发送requestString
os = hc.openDataOutputStream();
byte[] searchStringBody = searchString.getBytes();
for(int i = 0; i < searchStringBody.length; i++)
os.writeByte(searchStringBody[i]);
os.flush();
os.close();
//用inputstream读取server39s响应
is = hc.openInputStream();
//用Aelfred XML解析器解析此响应
try{
Parser parser = ParserFactory.makeParser("com.microstar,xml.SAXDriver");
//BookHandler解析由服务器返回的XML串
//并将结果返回至一个Vector中
BookHandler myHandler = new BookHandler();
parser.setDocumentHandler(myHandler);
InputSource inputSource = new InputSource(is);
//开始XML解析
parser.parse(inputSource);
bookList = myHandler.getBooks();
}catch(Exception se){
System.err.println("XML Error:" + se);
}
}finally{
//释放I/O流和http连接
if (hc != null) hc.close();
if (is != null) is.close();
if (os != null) os.close();
}
return bookList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -