📄 tiptestmidlet.java
字号:
// create StringItem showing tip name
StringItem tipNameItem =
new StringItem( "Tip Name:\n", list[ 1 ] );
// create StringItem showing tip description
StringItem tipDescriptionItem =
new StringItem( "\nTip Description:\n", list[ 2 ] );
// append StringItems to Form
answerScreen.append( tipNameItem );
answerScreen.append( tipDescriptionItem );
answerScreen.addCommand( nextCommand );
// allow soft button access for this Screen
answerScreen.setCommandListener(
new CommandListener() {
// invoked when user presses soft button
public void commandAction(
Command command, Displayable displayable )
{
// get next question
String data = getServerData( servletBaseURL +
tipTestServletName );
// display next question
display.setCurrent( createTipTestScreen(
servletBaseURL + data ) );
}
} // end anonymous inner class
);
return answerScreen;
} // end method createAnswerScreen
// sends user's test selection to servlet
private String postData( int selection )
{
// connect to server, then post data
try {
// connect to server sending User-Agent header
HttpConnection httpConnection =
( HttpConnection ) Connector.open(
servletBaseURL + tipTestServletName,
Connector.READ_WRITE );
setUserAgentHeader( httpConnection );
// send sessionID, if one exists
if ( sessionID != null )
httpConnection.setRequestProperty(
"cookie", sessionID );
// inform servlet of post request
httpConnection.setRequestMethod( HttpConnection.POST );
// open output stream to servlet
DataOutputStream out =
httpConnection.openDataOutputStream();
// send selection
out.writeUTF( Integer.toString( selection ) );
out.flush();
// get result from servlet
String data = getData( httpConnection );
httpConnection.close(); // close connection
return data;
} // end try
// handle if MIDlet cannot open HTTP connection
catch ( IOException ioException ) {
ioException.printStackTrace();
}
return null;
} // end method postData
// string tokenizer parses String into sub-string array
private String[] parseData( String data, char delimiter )
{
int newLines = 0;
// determine number of delimiter characters in String
for ( int i = 0; i < data.length(); i++ )
// increase number of delimiters by one
if ( data.charAt( i ) == delimiter )
newLines++;
// create new String array
String list[] = new String[ newLines ];
int oldNewLineIndex = 0;
int currentNewLineIndex;
// store Strings into array based on demiliter
for ( int i = 0; i < newLines; i++ ) {
// determine index where delimiter occurs
currentNewLineIndex =
data.indexOf( delimiter, oldNewLineIndex );
// extract String within delimiter characters
list[ i ] = data.substring( oldNewLineIndex,
currentNewLineIndex - 1 );
oldNewLineIndex = currentNewLineIndex + 1;
}
return list;
} // end method parseData
// connect to server and receive data
private String getServerData( String serverUrl )
{
// connect to server, then get data
try {
// connect to server sending User-Agent header
HttpConnection httpConnection =
( HttpConnection ) Connector.open( serverUrl );
setUserAgentHeader( httpConnection );
// send sessionID to server
if ( sessionID != null )
httpConnection.setRequestProperty(
"cookie", sessionID );
// get sessionID from server
String sessionIDHeaderField =
httpConnection.getHeaderField( "Set-cookie" );
// store sessionID from cookie
if ( sessionIDHeaderField != null ) {
int index = sessionIDHeaderField.indexOf( ';' );
sessionID =
sessionIDHeaderField.substring( 0, index );
}
// receive server data
String data = getData( httpConnection );
httpConnection.close(); // close connection
return data;
} // end try
// handle exception communicating with HTTP server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
return null;
} // end method getServerData
// downloads an image from a server
private Image getServerImage( String imageUrl )
{
// download image
try {
// open connection to server
HttpConnection httpConnection =
( HttpConnection ) Connector.open( imageUrl );
int connectionSize = ( int ) httpConnection.getLength();
byte imageBytes[] = new byte[ connectionSize ];
// read image from server
InputStream input = httpConnection.openInputStream();
input.read( imageBytes );
// create image from imageBytes
return
Image.createImage( imageBytes, 0, connectionSize );
}
// handle exception if InputStream cannot input bytes
catch ( IOException ioException ) {
ioException.printStackTrace();
}
return null;
} // end method getServerImage
// set User-Agent header to identify client to servlet
private void setUserAgentHeader(
HttpConnection httpConnection )
{
// set User-Agent header
try {
// use profile/configuration properties for User-Agent
String userAgentHeader = "Profile=" +
System.getProperty( "microedition.profiles" ) +
" Configuration=" +
System.getProperty( "microedition.configuration" );
// set header
httpConnection.setRequestProperty(
"User-Agent", userAgentHeader );
}
// handle exception getting request property
catch ( IOException ioException ) {
ioException.printStackTrace();
}
} // end method setUserAgentHeader
// open DataInputStream to receive data
private String getData( HttpConnection httpConnection )
throws IOException
{
String data = ""; // stores data
// open input stream from connection
DataInputStream dataInputStream =
new DataInputStream(
httpConnection.openInputStream() );
int inputCharacter = dataInputStream.read();
// read all data
while ( inputCharacter != -1 ) {
data = data + ( char ) inputCharacter;
inputCharacter = dataInputStream.read();
}
dataInputStream.close(); // close stream
return data;
} // end method getData
}
/***************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have *
* used their best efforts in preparing the book. These *
* efforts include the development, research, and testing of *
* the theories and programs to determine their effectiveness. *
* The authors and publisher make no warranty of any kind, *
* expressed or implied, with regard to these programs or to *
* the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for *
* incidental or consequential damages in connection with, or *
* arising out of, the furnishing, performance, or use of *
* these programs. *
***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -