J2ME Learning Notes (6) ----- Connect MIDlet to Text Files and Search Data Huanghui22 [Original]

zhaozj2021-02-16  104

Connect MIDlet to text files and retrieve data

1. Connection category 1 in J2ME 1) J2ME, network connection is processed by the gentric connection framework (GCF), which is a set of APIs, which has a class and eight interfaces. GCF resides in the javax.microedition.IO package. 2) Advantages of CGF: --- Added consistency supporting different types of network protocols; --- definition and use more reliable and expandable new protocols; - increasing compatibility with standard Java technology. 3) GCF comprises a class --- (Connector) --- exception (ConnectionNotFoundException) --- Interface (Connection, DatagramConnection, StreamConnectionNotifier, InputConnection, InputConnection, StreamConnection, ContentConnection) 4) J2ME microedition.io packet replacing with the J2SE java .NET package.

2. I / O class 1 in J2ME 1) J2ME I / O class library supported by Java.IO Package 2) For example, using Reader and Writer class to process character streams to use Inputerstream and OutputStream class to process byte streams

3. Example: 1) Task Statement: SaveMyMoney Bank Application Retrieves text files stored on the J2EE server and randomly displays content in the text on your phone screen 2) Development steps: a .---- - open Notepad, writing as the following code: The keyword to know the current balance is SMMBCBAL The keyword to know the check status is SMMBCHKS The keyword to obtain mini statement is SMMBMINI The keyword to know the fixed deposit details is SMMBFDDT The... keyword to request for checkbook is SMMBBOOK. the keyword to stop check transaction is SMMBSTOP. the keyword to request for bill presentation is SMMBBILL. the keyword to request for help is SMMBHELP. save as keyword.txt, and put the file into the J2EE server In the PUBLIC_HTML folder, the objects retrieved as the SaveMoney bank application. (The premise is that you must install the J2EE server, you can view J2EE related information).

b .----- Write the code as follows:

Import javax.microedition.midlet. *; import javax.microedition.lcdui. *; import java.io. *; //javax.microedition.IO package contains classes and interfaces to connect to the MIDlet to network resources, // If you want to establish a two-way connection between MIDlets and text files, you can use the StreamConnection interface. / / You can use the HTTP connection to retrieve data in the text file stored in the J2EE server. *; Import Java.util. *;

public class SaveMyMoney extends MIDlet implements CommandListener {private Command exitCommand, nextCommand; private Display display; private Form form; private StringItem keyWord; private Vector keyVector; public SaveMyMoney () {display = Display.getDisplay (this); exitCommand = new Command ( " EXIT ", command.exit, 2); NextCommand = new command (" next ", command.ok, 2); form = new form (" smmb keywords help "); keyword = new stringItem (" We Help " ); Ticker Ticker = new Ticker ("Want to Know Your Balance, Check Status, Transaction Details, or Bill Details? User Keywords to Bank with US); Form.Setticker (Ticker); Form.Append (keyword); Form.Append (keyword); .addCommand (exitCommand); form.addCommand (nextCommand); form.setCommandListener (this); keyVector = new Vector ();} public void startApp () throws MIDletStateChangeException {display.setCurrent (form); readKeyword (); showKey Word ();

Public void pauseapp () {}

Public void destroyApp (boolean unconditional) {}

public void commandAction (Command c, Displayable d) {if (c == exitCommand) {destroyApp (false); notifyDestroyed ();} else if (c == nextCommand) {showKeyword ();}} private void readKeyword () { StreamConnection Connect = NULL; // Creating a stream to retrieve data inputStream instream = null; // Create a string buffer StringBuffer buffer buffer stored in the retrieved data; try {// Establish http and store The keyword.txt file in the J2EE server is set to the StreamConnection type so that the input and output stream can be sent. Connect = (streamconnection) Connector.Open ("http: // localhost: 8000 / keyword. TXT "); // OpenInputStream method Open the connection input stream instream = connect.openputStream (); int ic; // Retrieve data with the read () method, return -1 to the end of the text file, the While loop Terminate While ((Input" = Instream.read ())! = -1) {// buffer once stores a line of text if (Input! = '/ n') {buffer.Append ((char) INPUT);} else {// Text is passed Vector keycent keyvector.addelement (buffer. toString ()); buffer = new StringBuffer ();}}} catch (IOException e) {System.err.println ( "the connection could not be established sorry for the inconvenience.");}} private void showKeyword () { // Randomly select a line of text from the vector, store this row in the StringItem object called Keyword, and then display this line Random Random = New Random (Calendar.GetInstance (). GetTime (). Gettime )); Int position = math.abs (random.nextint ())% keyVector.Size (); keyverctor.etext (position));}} c. Run J2EE server, in command prompt Enter the command j2ee -verbose d. Open KToolbar, create new projects ---- Click Build to compile, pre-verification, and packages - Click RUN to test

转载请注明原文地址:https://www.9cbs.com/read-10564.html

New Post(0)