📄 mobile datamanager.java
字号:
/*--------------------------------------------------
* DataManager.java
*
* Use GET to communicate with a Java servlet.
* The servlet will Submit Data to a database
*
* BIT-001-0141/2006 NJERU NGANATHA ANDREW
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class DataManager extends MIDlet implements CommandListener
{
private Image splashLogo;
private boolean isSplash = true;
private Display display; // Reference to Display object
private Form fmMain; // The main form
private Form fmMenu; // The Menu form
private Alert alError; // Alert to error message
private Command cmSubmit; // Request method GET
private Command cmHelp;
private Command cmAboutUs;
private Command cmExit;
private TextField tfSur;
private TextField tfFst;
private TextField tfAge;
private TextField tfGdr;
private TextField tfRst;
private StringItem siBalance;// Show account balance
private StringItem siResult;
private String errorMsg = null;
private Thread th = null;
Ticker t;
public DataManager()
{
display = Display.getDisplay(this);
// Create commands
cmSubmit = new Command("Submit", Command.SCREEN, 2);
cmHelp = new Command("Help", Command.SCREEN, 3);
cmAboutUs = new Command("AboutUs", Command.SCREEN, 4);
cmExit = new Command("Exit", Command.EXIT, 1);
// Textfields
tfSur = new TextField("Surname:", "", 25, TextField.ANY | TextField.ANY);
tfFst = new TextField("Firstname:", "", 25, TextField.ANY | TextField.ANY);
tfAge = new TextField("Age:", "", 2, TextField.NUMERIC);
tfGdr = new TextField("Gender: M-Male or F-Female:", "", 1, TextField.ANY | TextField.ANY);
tfRst = new TextField("Favourite TV Channel:", "", 25, TextField.ANY | TextField.ANY);
// Create Form, add commands & componenets, listen for events
fmMain = new Form("Data Upload Manager");
fmMain.addCommand(cmExit);
fmMain.addCommand(cmSubmit);
fmMain.addCommand(cmHelp);
fmMain.addCommand(cmAboutUs);
fmMain.append(tfSur);
fmMain.append(tfFst);
fmMain.append(tfAge);
fmMain.append(tfGdr);
fmMain.append(tfRst);
fmMain.setCommandListener(this);
}
public void startApp()
{
t=new Ticker("Welcome, Enter Data and Click Submit..");
fmMain.setTicker(t);
if(isSplash) {
isSplash = true;
try {
splashLogo = Image.createImage("/image/splash.png");
new SplashScreen(display, fmMain, splashLogo,5000);
} catch(Exception ex) {
}
}
try {
Image screen = Image.createImage("/image/screen.png");
fmMain.append(screen);
} catch (IOException e) {
}
}
public void clearFields() throws IOException
{
tfSur.setString("");
tfFst.setString("");
tfAge.setString("");
tfGdr.setString("");
tfRst.setString("");
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command c, Displayable s)
{
if (c == cmSubmit || c == cmHelp || c == cmAboutUs)
{
try
{
if (c == cmSubmit)
{
Thread th = new Thread()
{
public void run()
{
try
{
lookupBalance_withGET();
clearFields();
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
};
th.start();
}
else if (c == cmHelp)
{
Thread th = new Thread()
{
public void run()
{
try
{
splashLogo = Image.createImage("/image/Help.png");
new SplashScreen(display, fmMain, splashLogo,9000);
//lookupBalance_withPOST();
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
};
th.start();
}
else
{
Thread th = new Thread()
{
public void run()
{
try
{
splashLogo = Image.createImage("/image/info.png");
new SplashScreen(display, fmMain, splashLogo,9000);
//lookupBalance_withPOST();
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
};
th.start();
}
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
/*--------------------------------------------------
* Access servlet using GET
*-------------------------------------------------*/
private void lookupBalance_withGET() throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;
boolean ret = false;
// Data is passed at the end of url for GET
String url = "http://localhost:8080/DataManager/DataManagerServlet" + "?" +
"surname=" + tfSur.getString() + "&" +
"firstname=" + tfFst.getString() + "&" +
"age=" + tfAge.getString() + "&" +
"gender=" + tfGdr.getString() + "&" +
"results=" + tfRst.getString();
try
{
http = (HttpConnection) Connector.open(url);
//----------------
// Client Request
//----------------
// 1) Send request method
http.setRequestMethod(HttpConnection.GET);
// 2) Send header information - none
// 3) Send body/data - data is at the end of URL
//----------------
// Server Response
//----------------
iStrm = http.openInputStream();
// Three steps are processed in this method call
ret = processServerResponse(http, iStrm);
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
// Process request failed, show alert
if (ret == false)
showAlert(errorMsg);
}
/*--------------------------------------------------
* Process a response from a server
*-------------------------------------------------*/
private boolean processServerResponse(HttpConnection http, InputStream iStrm) throws IOException
{
//Reset error message
errorMsg = null;
// 1) Get status Line
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
// 2) Get header information - none
// 3) Get body (data)
int length = (int) http.getLength();
String str;
if (length != -1)
{
byte servletData[] = new byte[length];
iStrm.read(servletData);
str = new String(servletData);
}
else // Length not available...
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
str = new String(bStrm.toByteArray());
bStrm.close();
}
// Update the string item on the display
siBalance.setText(str);
return true;
}
else
// Use message from the servlet
errorMsg = new String( http.getResponseMessage());
return false;
}
/*--------------------------------------------------
* Show an Alert
*-------------------------------------------------*/
private void showAlert(String msg)
{
// Create Alert, use message returned from servlet
alError = new Alert("Successful Entry!", msg, null, AlertType.ERROR);
// Set Alert to type Modal
alError.setTimeout(Alert.FOREVER);
// Display the Alert. Once dismissed, display the form
display.setCurrent(alError, fmMain);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -