📄 sept01_ericg.txt
字号:
J 2 M E T E C H T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
WELCOME to the Java Developer Connection(sm) (JDC)
Java(tm) 2 Platform, Micro Edition (J2ME(tm))
Tech Tips, for September 17, 2001. This issue covers:
* Making HTTP Connections Using Background Threads
* Using MIDP Gauges
The J2ME Tech Tips are written by Eric Giguere
(http://www.ericgiguere.com), an engineer at iAnywhere
Solutions, inc. Eric is the author of the book "Java 2 Micro
Edition: Professional Developer's Guide" and co-author of the
upcoming "Mobile Information Device Profile for Java 2 Micro
Edition," both books in John Wiley & Sons' Professional
Developer's Guide series.
You can view this issue of the J2ME Tech Tips on the Web at
http://java.sun.com/jdc/J2METechTips/2001/tt0917.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MAKING HTTP CONNECTIONS USING BACKGROUND THREADS
All user interface frameworks share some basic concepts. This is
true no matter if the UI framework is the LCDUI classes defined
by the Mobile Information Device Profile (MIDP), or the AWT and
Swing classes defined by Java(tm) 2 Standard Edition (J2SE(tm)).
One of the basic concepts is that the user interface must always
be responsive to the user's interaction. In other words, if the
user presses a button, moves a pointing device, or performs some
other input operation, the user interface should act on that
input as soon as possible. The usual rule of thumb is that the
application should respond to input no later than a tenth of a
second after it occurs. Longer delays are noticed by the user, and
make the user interface seem unresponsive.
The key to user interface responsiveness, then, is to not do more
than about a tenth of a second's worth of work in response to any
user interface event. Why? When the application first starts,
the user interface subsystem creates a thread to wait for and
service input events. This thread, known as the event thread,
invokes application-defined methods whenever events occur.
In the MIDP, for example, the current screen's registered command
listener is called when a command is triggered. Or if the current
screen is a canvas, its keyPressed method is called whenever a
key is pressed. The event thread cannot process further events
until the invoked method (referred to as an event method) returns.
If the event method performs a lengthy operation, the user
interface is effectively blocked from processing further input,
and sometimes even from repainting the display. The longer an
event method takes, the more unresponsive the user interface
seems. (Please note that this is a simplified description of user
interface event handling -- the reality is a bit more complex.
The description is accurate enough for our purposes.)
Responsiveness aside, there's another reason to avoid lengthy
operations in response to events: you can't cancel an operation
because the event thread is busy and is unable to process further
user input.
So how do you ensure responsiveness and give users a way to
cancel an operation? Simple: use worker threads to perform long
operations, leaving the event thread free to handle user input
and to repaint the display.
For a concrete example, consider an HTTP connection made using
the MIDP's HttpConnection class. Simply connecting to the server
can take several seconds from a wireless device, because at the
present time wireless connections are very slow. Add to that the
time for the server to respond and for the response to be
received and processed by the application, and you can see that
it's not unusual for an HTTP request and response cycle to take
ten seconds or more. It makes sense, then, to display some kind
of status message as the connection is made, and allow the user
to cancel the connection entirely. Below is a simple class
that does just that: it uses a separate thread to connect to a
Web server and process the output. While the class does this, it
displays the current status on a form, and allows the user to
cancel the operation. You can use this simple application to see
what HTTP headers are being sent back to the device when it
connects to a specific Web server.
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
// Simple class that connects to a given URL and
// prints out the response code and any headers
// that are sent back. Shows how to use background
// threads to make HTTP connections while keeping the
// user interface alive.
public class HttpLogger extends MIDlet {
private Display display;
private Command exitCommand =
new Command( "Exit", Command.EXIT, 1 );
private Command okCommand =
new Command( "OK", Command.OK, 1 );
private Command cancelCommand =
new Command( "Cancel", Command.CANCEL, 1 );
private URLEntry mainForm;
// Standard MIDlet stuff....
public HttpLogger(){
}
protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}
protected void pauseApp(){
}
protected void startApp()
throws MIDletStateChangeException {
if( display == null ){
initMIDlet();
}
}
private void initMIDlet(){
display = Display.getDisplay( this );
mainForm = new URLEntry();
display.setCurrent( mainForm );
}
public void exitMIDlet(){
notifyDestroyed();
}
// Utility routine to display exceptions.
void displayError( Throwable e, Displayable next ){
Alert a = new Alert( "Exception" );
a.setString( e.toString() );
a.setTimeout( Alert.FOREVER );
display.setCurrent( a, next );
}
// Simple entry form for entering the URL to
// connect to.
class URLEntry extends TextBox
implements CommandListener {
URLEntry(){
super( "Enter URL:", "java.sun.com",
100, 0 );
addCommand( exitCommand );
addCommand( okCommand );
setCommandListener( this );
}
public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
exitMIDlet();
} else if( c == okCommand ){
try {
HttpConnection conn =
(HttpConnection)
Connector.open( "http://" +
getString() );
display.setCurrent(
new Logger( this, conn ) );
}
catch( IOException e ){
displayError( e, this );
}
}
}
}
// Simple form that contains a single string item.
// The string item is updated with status messages.
// When constructed, it starts a background thread
// that makes the actual HTTP connection. Displays
// a Cancel command to abort the current connection.
class Logger extends Form
implements Runnable, CommandListener {
private Displayable next;
private HttpConnection conn;
private StringItem text =
new StringItem( null, "" );
Logger( Displayable next,
HttpConnection conn ){
super( "HTTP Log" );
this.next = next;
this.conn = conn;
addCommand( cancelCommand );
setCommandListener( this );
append( text );
Thread t = new Thread( this );
t.start();
}
// Handle the commands. First thing to do
// is switch the display, in case closing/
// the connection takes a while...
public void commandAction( Command c,
Displayable d ){
display.setCurrent( next );
try {
conn.close();
}
catch( IOException e ){
displayError( e, next );
}
}
// Do the connection and processing on
// a separate thread...
public void run(){
update( "Connecting to " + conn.getURL() );
try {
int rc = conn.getResponseCode();
update( "Response code: " + rc );
update( "Response message: " +
conn.getResponseMessage() );
String key;
for( int i = 0;
( key =
conn.getHeaderFieldKey( i ) )
!= null;
++i ){
update( key + ": " +
conn.getHeaderField( i ) );
}
}
catch( IOException e ){
update( "Caught exception: " +
e.toString() );
}
removeCommand( cancelCommand );
addCommand( okCommand );
}
// Update the string item with new information.
// Only do it if we're actually visible.
void update( String line ){
if( display.getCurrent() != this ) return;
String old = text.getText();
StringBuffer buf = new StringBuffer();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -