J2ME with Web Service

xiaoxiao2021-03-06  67

Service

This time the Web Service to be released is very simple. Its function is to transform lowercase letters in the string of the client into uppercase letters and return to the client. The SOAP server uses Apache's AXIS (you can download from http://ws.apache.org/Axis/), the application server can choose a variety of servlet containers, I use WebLogic.

1.1 Implement the source code of the class

// StringProcessor.java

Package com.jagie.j2me.ws;

Public class stringprocessor {

Public stringprocessor () {

}

Public String Process (String Name) {

Return name.touppercase ();

}

}

1.2 release step

1. Prepare a directory as a publishing directory of Web Application, I am here, this directory is jagiews. It is best not to have spaces and Chinese in the full path of this directory. My release directory structure is as follows:

2. Compile StringProcessor.java, put the generated stringprocessor.class in: / jagiews / web-inf / class / coOM / JAGIE / J2ME / WS directory.

3. Place the JAR file required by the following AXIS server in the JAGIEWS / WEB-INF / LIB folder, Axis-Ant.jar, Commons-Discovery.jar, Commons-Logging.jar, Jaxrpc.jar, Log4j- 1.2.8.jar, saaj.jar, wsdl4j.jar. These files can be downloaded at http://ws.apache.org/axis/, as shown:

4. Add 2 release description files in the jagiews / web-infroductory: Server-config.wsdd, web.xml.

# Server-config.wsdd

XMLns: java = "http://xml.apache.org/axis/wsdd/providers/java">

Value = "org.apache.axis.attachments.attachmentsImpl" />

TYPE = "java: org.apache.axis.transport.local.localResponder" />

TYPE = "java: org.apache.axis.handlers.http.urmapper" />

Type = "java: org.apache.axis.providers.java.rpcprovider" />

TYPE = "java: org.apache.axis.handlers.simpleauthenticationHandler" />

TYPE = "java: org.apache.axis.providers.java.msgprovider" />

http://xml.apache.org/axis/wsdd/

Value = "com.jagie.j2me.ws.stringprocessor" />

# Web.xml

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

apache-axis

Axisservlet

apache-axis servlet

Org.apache.axis.transport.http.axisservlet

adminServlet

Axis Admin Servlet

Org.apache.axis.transport.http.adminservlet

100

SOAPMONITORSERVICE

SOAPMONITORSERVICE

Org.apache.axis.monitor.soapmonitorService

SOAPMonitorport

5001

100

Axisservlet

/ servlet / axisservlet

Axisservlet

*. jws

Axisservlet

/ services / *

SOAPMONITORSERVICE

/ soapmonitor

adminServlet

/ servlet / adminservlet

->

http://www.w3.org/tr/2003/wd-wsdl12-20030303/#ietf-draft

For now We go with the basic 'it's xml' response ->

WSDL

text / xml

XSD

text / xml

5. Turn on your Application Server, publish the directory jagiews as a web application called Jagiews.

6. Test: Open the browser, enter the URL (WebLogic, other servers, please modify it as appropriate): http: // localhost: 7001 / jagiews / services / stringprocess? Method = process & name = qqqq, if the browser can The returned XML document displays the string "QQQQQ", congratulations, your web service is successful. If the release is unsuccessful, press the above release procedure to check.

2. Client

The client is naturally used by MIDlet, but what way to access the web service? In fact, there are three ways to access

Access the http: // localhost: 7001 / jagiews / services / stringprocess? Method = process & name = qqq directly, get the return data of XML, then resolved with KXML (http://kxml.enhydra.org/) to get the return value . If your phone supports MIDP2.0, you can consider using JSR172. Use the KSOAP API.

Here is the third way. Before using, you need to download a stable KSOAP package from http://ksoap.enhydra.org/software/downloads/index.html, placed in your classpath.

2.1 client source code

2.1.1 wsclientmidlet.java

Package com.jagie.j2me.ws;

Import javax.microedition.midlet. *;

Import javax.microedition.lcdui. *;

/ **

*

Title:

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author NOT Attributable

* @version 1.0

* /

Public class wsclientmidlet

Extends midlet {

Static WSClientMidlet installation;

Public wsclientmidlet () {

Instance = THIS;

}

Public void startapp () {

Display Display = Display.getDisplay (this);

Displayform Displayable = New DisplayForm ();

Display.Setcurrent (Displayable);

}

Public void pauseApp () {

}

Public void destroyApp (boolean unconditional) {}

Public static void quitApp () {

Instance.destroyApp (True);

Instance.notifyDestroyed ();

Instance = NULL;

}

}

2.1.2 DisplayForm.java

Package com.jagie.j2me.ws;

Import javax.microedition.lcdui. *;

/ **

*

Title:

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author NOT Attributable

* @version 1.0

* /

Public Class DisplayForm

Extends Form

Implements commandListener, runnable {

Private textfield textfield1;

Private thread t;

Public DisplayForm () {

Super ("Character Conversion WebService Test");

Try {

Jbinit ();

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

Private void jbinit () throws exception {

// set up this displayable to listen to command Events

TextField1 = New TextField (",", ", 15, textfield.any);

This.SetCommandListener (this);

TextField1.setLabel ("The string to be processed is:");

Textfield1.SetConstraints (TextField.Any);

TextField1.setInitialInputMode ("Tester");

SetCommandListener (this);

// add the exit command

Addcommand (New Command ("EXIT", Command.exit, 1);

AddCommand (New Command ("Process", Command.ok, 1);

THIS.APPEND (TextField1);

}

Public void CommandAction (Command Command, Displayable DisplayAble) {

IF (Command.getCommandType () == command.exit) {

WSClientMidlet.quitApp ();

}

Else IF (Command.getCommandType () == command.ok) {

T = New Thread (this);

T.Start ();

}

}

Public void run () {

String s1 = textfield1.getstring ();

String S2 = new stringProcessorstub (). Process (S1);

StringItem ResultItem = New StringItem ("The string after processing is:", S2);

This.Append (ResultItem);

}

2.1.3 StringProcessorstub.java

Package com.jagie.j2me.ws;

Import org.ksoap. *;

Import Org.Ksoap.Transport.httpTransport;

/ **

*

Title:

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author NOT Attributable

* @version 1.0

* /

Public class stringprocessorstub {

Public stringprocessorstub () {

}

Public String Process (String Name) {

String result = null;

Try {

SOAPOBJECT RPC = New SOAPOBJECT

("http: // localhost: 7001 / jagiews / services / StringProcess", "process");

Rpc.addproperty ("Name", Name);

HttpTransport ht = new httptransport

("http: // localhost: 7001 / jagiews / services / stringprocess", "");

Result = (string) HT.Call (RPC);

}

Catch (Exception E) {

E.PrintStackTrace ();

}

Return Result;

}

}

Test client

Now, try to run the wsclientmidlet in your IDE, if the call is successful, then the following screen appears:

to sum up

With KSOAP, calling Web Service on your phone is easy. However, it is important to note that when using the network to connect this fee, be sure to do it separately, do not write directly in the commandAction () method, otherwise the screen is locked.

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

New Post(0)