📄 httpwrappermidlet.java
字号:
/* License
*
* Copyright 1994-2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
package httpwrapper;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// A simple MIDlet that does nothing except prompt for a URL and then
// connects to it and reads the data it sends back. The connection is logged
// using the HttpConnectionWrapper.
public class HttpWrapperMIDlet extends MIDlet implements CommandListener, Runnable {
private Display display;
private String url = "http://localhost:8084/httpfiltertest/";
public static final Command exitCommand = new Command( "Exit", Command.EXIT, 1 );
public static final Command okCommand = new Command( "OK", Command.OK, 1 );
public HttpWrapperMIDlet(){
}
// Process commands. If the OK command is invoked,
// need to determine which screen was active.
public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
exitMIDlet();
} else if( c == okCommand &&
d instanceof Prompter ){
// Start the page loading....
url = ((Prompter) d).getString();
if( url.indexOf( "://" ) == -1 ){
url = "http://"+ url;
}
display.setCurrent( new Wait() );
} else if( c == okCommand &&
d instanceof Lister ){
display.setCurrent( new Prompter() );
}
}
protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}
// Displays an error message using an alert.
private void error( String title, String msg,
Throwable e ){
Alert a = new Alert( title );
if( msg == null && e != null ){
msg = "Exception:";
}
a.setString( msg + " " +
( e != null ? e.toString() : "" ) );
a.setType( AlertType.ERROR );
a.setTimeout( Alert.FOREVER );
display.setCurrent( a, new Prompter() );
}
public void exitMIDlet(){
notifyDestroyed();
}
public Display getDisplay(){ return display; }
// Start by prompting the user...
protected void initMIDlet(){
display.setCurrent( new Prompter() );
}
protected void pauseApp(){
}
// Does the actual parsing. Invoked indirectly by
// the wait screen using display.callSerially.
// Loads the page and then parses it for the META
// tags.
public void run(){
Wait w = (Wait) display.getCurrent();
HttpConnection conn = null;
InputStream in = null;
try {
conn = new HttpConnectionWrapper( (HttpConnection) Connector.open( url ) );
conn.setRequestProperty( "User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
conn.setRequestProperty( "Connection", "close" );
conn.setRequestProperty( "Send-HTTP-Log-To", "ericgiguere@ericgiguere.com" );
int rc = conn.getResponseCode();
w.update( "Response code " + rc );
in = conn.openInputStream();
for( int i = 0; i < 100; ++i ){
if( in.read() == -1 ) break;
}
display.setCurrent( new Prompter() );
}
catch( IOException e ){
error( "Exception", "Error reading page", e );
}
finally {
if( in != null ){
try { in.close(); } catch( IOException e ){}
}
if( conn != null ){
try { conn.close(); } catch( IOException e ){}
}
}
}
protected void startApp()
throws MIDletStateChangeException {
if( display == null ){
display = Display.getDisplay( this );
initMIDlet();
}
}
// A simple class that prompts for a URL.
class Prompter extends TextBox {
Prompter(){
super( "Enter a URL:", url,
200, 0 );
addCommand( okCommand );
addCommand( exitCommand );
setCommandListener( HttpWrapperMIDlet.this );
}
}
// Displays our list of tags. The tags are
// added to the list in the run method.
class Lister extends List {
Lister(){
super( "Meta tags:", List.IMPLICIT );
addCommand( okCommand );
setCommandListener( HttpWrapperMIDlet.this );
}
}
// Asks the user to the wait while a lengthy
// operation takes place. The operation does not
// start until the canvas has been painted once,
// at which point display.callSerially is invoked
// to start it.
class Wait extends Canvas {
boolean called = false;
String msg = "Connecting...";
// Draw the message on screen.... fancier code
// would center and wrap the text...
protected void paint( Graphics g ){
g.setColor( 255, 255, 255 );
g.fillRect( 0, 0, getWidth(), getHeight() );
g.setColor( 0, 0, 0 );
g.drawString( msg, 0, 0,
Graphics.TOP | Graphics.LEFT );
if( !called ){
Thread t = new Thread( HttpWrapperMIDlet.this );
t.start();
called = true;
}
}
// Update the string and immediately repaint
// the screen.
public void update( String newMsg ){
msg = newMsg;
repaint( 0, 0, getWidth(), getHeight() );
serviceRepaints();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -