Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید




A.4 Code Walk Through


The sample application in this appendix demonstrates the use of the above MIDP programming model and APIs. The sample application allows the user to enter a URL. When the user clicks the Fetch button, the MIDlet fetches and displays the content from that URL. The MIDlet also stores the URL in RMS for future browsing. Figure A.1 shows the MIDlet in action. Listing A.1 shows the entire source code. Please note that the main MIDlet is also its own UI command listener and handler. The worker threads are inner classes extending the JDK Thread class.


Figure A.1. The sample MIDlet in action.


Listing A.1. The sample MIDlet


package com.enterprisej2me.simpledemo;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
public class DemoMIDlet extends MIDlet
implements CommandListener {
private Display display;
private Command fetch;
private Command history;
private Command erase;
private Command exit;
private Command done;
private TextField urlField;
private RecordStore store;
public DemoMIDlet () throws Exception {
display = Display.getDisplay(this);
fetch = new Command("FETCH", Command.SCREEN, 1);
history = new Command("HISTORY", Command.SCREEN, 1);
erase = new Command("ERASE", Command.SCREEN, 1);
exit = new Command("EXIT", Command.CANCEL, 1);
done = new Command("DONE", Command.CANCEL, 1);
store = RecordStore.openRecordStore("DataStore", true);
}
public void startApp() {
startScreen ();
}
public void pauseApp() {
// Nothing to do ...
}
public void destroyApp(boolean unconditional) {
try {
store.closeRecordStore ();
} catch (Exception exp) {
exp.printStackTrace ();
}
}
public void commandAction(Command command,
Displayable screen) {
if (command == exit) {
destroyApp(false);
notifyDestroyed();
} else if (command == fetch) {
FetchWorker t = new FetchWorker ();
t.setListener(this);
t.start();
} else if (command == history) {
HistoryWorker t = new HistoryWorker ();
t.setListener(this);
t.start();
} else if (command == erase) {
try {
store.closeRecordStore ();
RecordStore.deleteRecordStore("DataStore");
store =
RecordStore.openRecordStore("DataStore", true);
} catch (Exception exp) { }
HistoryWorker t = new HistoryWorker ();
t.setListener(this);
t.start();
} else if (command == done) {
startScreen();
}
}
private void startScreen () {
Form f = new Form ("Get content");
urlField =
new TextField ("URL: ", ", 30, TextField.ANY);
f.append (urlField);
f.addCommand (fetch);
f.addCommand (exit);
f.setCommandListener(this);
display.setCurrent(f);
}
class FetchWorker extends Thread {
private CommandListener listener;
public void setListener (CommandListener cl) {
listener = cl;
}
public void run () {
HttpConnection conn = null;
DataInputStream din = null;
ByteArrayOutputStream bos = null;
try {
// Get the url
String url = urlField.getString ();
// Fetch the remote content to a byte array "buf"
conn = (HttpConnection) Connector.open(url);
conn.setRequestMethod(HttpConnection.GET);
din = conn.openDataInputStream();
bos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
while (true) {
int rd = din.read(buf, 0, 256);
if (rd == -1) break;
bos.write(buf, 0, rd);
}
bos.flush();
buf = bos.toByteArray();
// Save the history
store.addRecord(url.getBytes(), 0,
url.getBytes().length);
// Display a new screen
Form f = new Form ("content");
f.append(url + "\n");
f.append(new String(buf));
f.addCommand(history);
f.addCommand(done);
f.setCommandListener (listener);
display.setCurrent(f);
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
if (din != null) din.close();
if (conn != null) conn.close();
if (bos != null) bos.close();
} catch (Exception exp) {}
}
}
}
class HistoryWorker extends Thread {
private CommandListener listener;
public void setListener (CommandListener cl) {
listener = cl;
}
public void run () {
try {
Form f = new Form ("History");
RecordEnumeration enu =
store.enumerateRecords(null, null, false);
for (; enu.hasNextElement() ;) {
byte [] data = enu.nextRecord();
f.append((new String(data)) + "\n");
}
enu.destroy();
f.addCommand(erase);
f.addCommand(done);
f.setCommandListener (listener);
display.setCurrent(f);
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
}


/ 204