J2ME Learning Notes (6) ----- Connect MIDlet to text files and retrieve data

zhaozj2021-02-16  87

Connect MIDlet to text files and retrieve data

1. Connection class in J2ME

1) In J2ME, the 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;

--- Define and use more reliable and expandable new protocols;

--- Increase compatibility with standard Java technology in class libraries.

3) GCF includes

--- Class (Connector)

--- Abnormal (ConnectionNotFoundException)

--- Connection, DataGramconnection, StreamConnection, InputConnection, StreamConnection, ContentConnection

4) J2ME uses microedition.IO package to replace J2SE Java.net package.

2. I / O class in J2ME

1) J2ME I / O class library supported by java.io package

2) For example, using Reader and Writer class processing character stream

Use the INPUTERSTREAM and OUTPUTSTREAM class to process byte streams

3. Example:

1) Task statement: The SaveMymoney bank application needs to retrieve the text file stored on the J2EE server and randomly displays the contents of a row in the text on the phone screen.

2) Development steps:

a .----- Open Notepad, write such 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 PUBLIC_HTML folder of the J2EE server, as an object 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 the classes and interfaces to connect to the MIDlet to the 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 the data of the text file stored in the J2EE server.

Import javax.microedition.io. *;

Import java.util. *; public class savemoney extends MIDlet imports

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? Used");

Form.Setticker (Ticker);

Form.Append (keyword);

Form.addcommand (exitcommand);

Form.addcommand (NextCommand);

Form.setCommandListener (this);

KeyVector = new vector ();

}

Public void startapp () throws MidletStateChangeException

{

Display.SetCurrent (Form);

Readkeyword ();

SHOWKEYWORD ();

}

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 vid readyyword ()

{

StreamConnection connect = NULL;

// Create an input stream to retrieve the data in the connection

InputStream Instream = NULL;

// Create a buffer that stores the retrieved data

StringBuffer buffer = new stringbuffer ();

Try

{

// Establish HTTP to connect to the keyword.txt file stored in the J2EE server

The // Connection object 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");

// OpenInInputStream method Opens the connection of the connection

INSTREAM = Connect.openputStream (); int.

// Retrieve data with the read () method, return to -1 to the end of the text file, the While loop termination

While ((input = instream.read ())! = -1)

{

// buffer once stored a line of text

IF (Input! = '/ n')

{

Buffer.Append ((char) input);

}

Else

{

// Text is passed to the vector keyVector

KeyVector.Addelement (buffer.tostring ());

Buffer = new stringbuffer ();

}

}

}

Catch (IOException E)

{

System.err.Println ("The Connection Could Not Be Established. Sorry for the Inconance");

}

}

Private void showkeyword ()

{

// Randomly select a line of text from the vector, store this row in the StringItem object called Keyword, then display this line on the mobile phone screen

Calendar.getInstance (). Gettime (). Gettime ());

INT position = math.abs (random.nextint ())% keyVector.size ();

Keyword.Settext (String) KeyVector.Elementat (position);

}

}

c. Run the J2EE server, click the command J2EE -VERBOSE at the command prompt

d. Open KToolbar, create new projects ---- Click Build to compile, prepare and package ---- Click RUN to test

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

New Post(0)