选择显示字体大小

j2me与web service-ksoap的罗曼史

j2meweb service-ksoap的罗曼史

----j2meweb service-ksoap的快速上手

作者:cleverpig


版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
原文地址:
http://www.matrix.org.cn/resource/article/43/43662_j2me_ksoap.html
关键词: j2me soap webservice





1、服务端

这次要发布的web service非常简单。它的功能是把从客户端传入的字符串中的小写字母转变成大写字母,再返回给客户端。

soap 服务器采用apache的axis(可以从http://ws.apache.org/axis/下载),应用服务器可以选用各种servlet 容器,我这里采用的是weblogic

1.1 实现类的源代码


// stringprocessor.java
package com.jagie.j2me.ws;

public class stringprocessor
{
  public stringprocessor()
  {
  }

  public string process(string name)
  {
    return name.touppercase();
  }

}


1.2 发布步骤

1.准备一个目录作为web application的发布目录,我这里的这个目录叫jagiews,这个目录的全路径中最好不要有空格和中文。我的发布目录结构如下:

2.编译stringprocessor.java,把生成的stringprocessor.class置于: \jagiews\web-inf\classes\com\jagie\j2me\ws目录下。

3.在jagiews\web-inf\lib 文件夹中置入以下axis服务器需要的jar文件 axis.jar,axis-ant.jar,commons-discovery.jar,commons-logging.jar,jaxrpc.jar,log4j-1.2.8.jar,saaj.jar ,wsdl4j.jar。这些文件可以在http://ws.apache.org/axis/下载。

4.在jagiews\web-inf目录下增加2个发布描述文件:
server-config.wsdd,web.xml

#server-config.wsdd

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<deployment xmlns=
&quot;http://xml.apache.org/axis/wsdd/&quot;
xmlns:java=
&quot;http://xml.apache.org/axis/wsdd/providers/java&quot;>
<globalconfiguration>
  <parameter name=
  &quot;adminpassword&quot; value=&quot;admin&quot;/>
  <parameter name=
  &quot;attachments.directory&quot;
  value=&quot;c:\program files
  \apache tomcat 4.0\webapps
  \axis\web-inf\attachments&quot;/>
  <parameter name=
  &quot;attachments.implementation&quot;
  value=&quot;org.apache.axis.
  attachments.attachmentsimpl&quot;/>
  <parameter name=
  &quot;sendxsitypes&quot;
  value=&quot;true&quot;/>
  <parameter name=
  &quot;sendmultirefs&quot;
  value=&quot;true&quot;/>
  <parameter name=
  &quot;sendxmldeclaration&quot;
  value=&quot;true&quot;/>
  <parameter name=
  &quot;axis.sendminimizedelements&quot;
  value=&quot;true&quot;/>
  <requestflow>
   <handler type=
   &quot;java:org.apache.axis.handlers.jwshandler&quot;>
    <parameter
name=&quot;scope&quot;
value=&quot;session&quot;/>
   </handler>
   <handler type=
   &quot;java:org.apache.axis.handlers.jwshandler&quot;>
    <parameter
name=&quot;scope&quot;
value=&quot;request&quot;/>
    <parameter
name=&quot;extension&quot;
value=&quot;.jwr&quot;/>
   </handler>
  </requestflow>
</globalconfiguration>
<handler name=
&quot;localresponder&quot;
type=&quot;java:org.apache.axis.
transport.local.localresponder&quot;/>
<handler name=&quot;urlmapper&quot;
type=&quot;java:org.apache.axis.
handlers.http.urlmapper&quot;/>
<handler name=&quot;rpcdispatcher&quot;
type=&quot;java:org.apache.axis.
providers.java.rpcprovider&quot;/>
<handler name=&quot;authenticate&quot;
type=&quot;java:org.apache.axis.
handlers.simpleauthenticationhandler&quot;/>
<handler name=&quot;msgdispatcher&quot;
type=&quot;java:org.apache.axis.
providers.java.msgprovider&quot;/>

<service name=&quot;adminservice&quot;
provider=&quot;java:msg&quot;>
  <parameter name=&quot;allowedmethods&quot;
  value=&quot;adminservice&quot;/>
  <parameter name=&quot;enableremoteadmin&quot;
  value=&quot;false&quot;/>
  <parameter name=&quot;classname&quot;
  value=&quot;org.apache.axis.utils.admin&quot;/>
  <namespace>
  http://xml.apache.org/axis/wsdd/
  </namespace>
</service>
<service name=&quot;version&quot;
provider=&quot;java:rpc&quot;>
  <parameter name=&quot;allowedmethods&quot;
  value=&quot;getversion&quot;/>
  <parameter name=&quot;classname&quot;
  value=&quot;org.apache.axis.version&quot;/>
</service>
<!--  your service  begin-->
  <service name=&quot;stringprocess&quot;
  provider=&quot;java:rpc&quot;>
  <parameter name=&quot;allowedmethods&quot;
  value=&quot;process&quot;/>
  <parameter name=&quot;classname&quot;
  value=&quot;com.jagie.j2me.
  ws.stringprocessor&quot;/>
</service>
<!-- your service  end -->
<transport name=&quot;http&quot;>
  <requestflow>
   <handler type=&quot;urlmapper&quot;/>
   <handler type=
   &quot;java:org.apache.axis.handlers.
   http.httpauthhandler&quot;/>
  </requestflow>
</transport>
<transport name=&quot;local&quot;>
  <responseflow>
   <handler type=&quot;java:org.apache.axis.
   transport.local.localresponder&quot;/>
  </responseflow>
</transport>
</deployment>

# web.xml

<?xml version=&quot;1.0&quot;
encoding=&quot;iso-8859-1&quot;?>

<!doctype web-app
    public &quot;-//sun microsystems,
inc.//dtd web application 2.3//en&quot;
    &quot;http://java.sun.com
/j2ee/dtds/web-app_2.2.dtd&quot;>

<web-app>
  <display-name>apache-axis</display-name>
  <servlet>
    <servlet-name>axisservlet</servlet-name>
    <display-name>apache-axis
servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.axisservlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>adminservlet</servlet-name>
    <display-name>axis admin servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.adminservlet
    </servlet-class>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>soapmonitorservice</servlet-name>
    <display-name>soapmonitorservice</display-name>
    <servlet-class>
        org.apache.axis.monitor.soapmonitorservice
    </servlet-class>
    <init-param>
      <param-name>soapmonitorport</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>axisservlet</servlet-name>
    <url-pattern>/servlet/axisservlet</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>axisservlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>axisservlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>soapmonitorservice</servlet-name>
    <url-pattern>/soapmonitor</url-pattern>
  </servlet-mapping>

<!-- uncomment this if you want the admin servlet -->
<!--
  <servlet-mapping>
    <servlet-name>adminservlet</servlet-name>
    <url-pattern>/servlet/adminservlet
</url-pattern>
  </servlet-mapping>
-->

    <!-- currently the w3c havent settled
on a media type for wsdl;
    http://www.w3.org/tr/2003/
wd-wsdl12-20030303/#ietf-draft
    for now we go with the basic
'it's xml' response -->
  <mime-mapping>
    <extension>wsdl</extension>
     <mime-type>text/xml</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
  </mime-mapping>
</web-app>




5.开启你的application server,把目录jagiews发布为一个名叫jagiews的web application。

6.测试:打开浏览器,输入网址(这里使用的是weblogic,其他的服务器请酌情修改): http://localhost:7001/jagiews/services/stringprocess?method=process&name=qqqq,如果浏览器能在返回的xml文档中显示字符串&quot;qqqq&quot;,恭喜你,你的web service发布成功了。如果发布不成功,请按以上发布步骤检查一下。

2、客户端

客户端自然是用midlet了,不过用什么方式来访问web service呢?其实有3种访问方式

直接用httpconnection访问 http://localhost:7001/jagiews/services/stringprocess?method=process&name=qqqq,得到xml的返回数据,然后用kxml(http://kxml.enhydra.org/)解析,得到返回值。

如果你的手机支持midp2.0的话,可以考虑使用jsr172。

用ksoap api。

这里讲述第三种方式。使用之前,你需要从 http://ksoap.enhydra.org/software/downloads/index.html下载稳定的ksoap包,置于你的classpath中。

2.1 客户端源代码

2.1.1 wsclientmidlet.java


package com.jagie.j2me.ws;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright: copyright (c)
2004</p>
* <p>company: </p>
* @author not attributable
* @version 1.0
*/

public class wsclientmidlet
    extends midlet
{
  static wsclientmidlet instance;

  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.*;
/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright: copyright (c)
2004</p>
* <p>company: </p>
* @author not attributable
* @version 1.0
*/

public class displayform
    extends form
    implements commandlistener,
runnable
{
  private textfield textfield1;
  private thread t;

  public displayform()
  {
    super(&quot;字符转换webservice测试&quot;);

    try
{
      jbinit();
    }
    catch (exception e)
{
      e.printstacktrace();
    }


  }

  private void jbinit()
  throws exception
  {
    // set up this displayable
to listen to command events
    textfield1 = new textfield
(&quot;&quot;, &quot;&quot;, 15, textfield.any);
    this.setcommandlistener(this);
    textfield1.setlabel
(&quot;待处理的字符串是:&quot;);
    textfield1.setconstraints
(textfield.any);
    textfield1.setinitialinputmode
(&quot;tester&quot;);
    setcommandlistener(this);
    // add the exit command
    addcommand(new command
(&quot;exit&quot;, command.exit, 1));
    addcommand(new command
(&quot;process&quot;, 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(&quot;处理后的字符串是:&quot;, s2);
    this.append(resultitem);
}
}




2.1.3 stringprocessorstub.java


package com.jagie.j2me.ws;

import org.ksoap.*;
import org.ksoap.transport.httptransport;

/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright: copyright (c) 2004</p>
* <p>company: </p>
* @author not attributable
* @version 1.0
*/

  public class stringprocessorstub
  {
    public stringprocessorstub()
{
  }

  public string process(string name)
  {
    string result = null;
    try
{

      soapobject rpc =
   new soapobject
    (&quot;http://localhost:
7001/jagiews/services/stringprocess&quot;,
&quot;process&quot;);

      rpc.addproperty(&quot;name&quot;, name);

      httptransport ht =
   new httptransport
    (&quot;http://localhost:7001/
jagiews/services/stringprocess&quot;,
&quot;&quot;);

      result = (string) ht.call(rpc);

    }
    catch (exception e) {
      e.printstacktrace();
    }

    return result;

  }
}


总结

有了ksoap,手机上调用web service就很容易了。不过要注意的是,使用网络连接这种费时操作的时候,一定要单独开线程进行,不要直接写在commandaction()方法里,否则出现画面被锁住的情况。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons