java web Service的使用

Web Service

Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

服务器端

包结构

|---com.zhiyuan.service
    |---ServiceHello.java

代码

ServiceHello.java

package com.zhiyuan.service;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class ServiceHello {
    /**
     * 
     * 供客户端调用的方法
     * @param name
     * @return String
     */
    public String getValue(String name) {
        return "我叫:" + name;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Endpoint.publish("http://localhost:80/Service/ServiceHello",
                new ServiceHello());
        System.out.println("service success!");

    }

}

客户端

axis方法(主推)

包结构

|---src
    |---test.java
|---lib
    |---activation.jar
    |---axis-ant.jar
    |---axis.jar
    |---commons-discovery-0.2.jar
    |---commons-logging-1.0.4.jar
    |---jaxrpc.jar
    |---log4j-1.2.8.jar
    |---mail.jar
    |---saaj.jar
    |---wsdl4j-1.5.1.jar
    |---xalan.jar
    |---xmlsec-1.2.1.jar

下载地址(主要包在这里):http://www.eu.apache.org/dist/axis/axis/java/1.4/axis-bin-1_4.zip

源码(缺少的包在这里):http://www.eu.apache.org/dist/axis/axis/java/1.4/axis-src-1_4.zip

test.java

package test;
import org.apache.axis.wsdl.WSDL2Java;


public class test {

        public static void main(String[] args) {  
              //-u是wsdl地址,-o是输出目录,
                String[] s = new String[] {  
                        "-u",  
                        "http://open.12301dev.com/openService/MXSE_beta.wsdl?wsdl",  
                        "-o", "src/client/", "-S", "false", "-t" };  
                WSDL2Java.main(s);  
            }
}

soap方法

包结构

|---test
    |---TestWebService.java
|---lib
    |---ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar

TestWebService.java

package test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


public class TestWebService {
    static final String METHOD_HELLO_WORLD = "Get_ScenicSpot_List"; 
    
    //服务器链接
    final static String WEB_SERVICE_URL = "http://open.12301dev.com/openService/pftMX.php";
    
    //final String WEB_SERVICE_URL = "http://open.12301dev.com/openService/MXSE_beta.wsdl";
    final static String Namespace = "http://open.12301dev.com/openService/pftMX.php";//命名空间
    /**
     * @param args
     */
    public static void main(String[] args) {
        invokeWebFunction();

    }
    
    public static void invokeWebFunction()
    {
        Map values = new HashMap();
        //values.put("ac", "100019");
        //values.put("pw", "jjl4yk11f82ce6c0f33a5c003f2fec56");
        values.put("ac", "100019");
        values.put("pw", "jjl4yk11f82ce6c0f33a5c003f2fec56");
        
        values.put("n", "1");
        values.put("m", "10");
        Request(METHOD_HELLO_WORLD,values);
    }
    /**
     * 执行异步任务
     * 
     * @param params
     *            方法名+参数列表(哈希表形式)
     */
    public static String Request(Object... params) {
    

        
        if (params != null && params.length == 2) {
            return CallWebService((String) params[0],
                    (Map) params[1]);
        } else if (params != null && params.length == 1) {
            return CallWebService((String) params[0], null);
        } else {
            return null;
        }

        
    }
    
    /**
     * 调用WebService
     * 
     * @return WebService的返回值
     * 
     */
    public static String CallWebService(String MethodName, Map Params) {
        // 1、指定webservice的命名空间和调用的方法名
        
        SoapObject request = new SoapObject(Namespace, MethodName);
        // 2、设置调用方法的参数值,如果没有参数,可以省略,
        if (Params != null) {
            Iterator iter = Params.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                request.addProperty((String) entry.getKey(),
                        (String) entry.getValue());
            }
        }
        // 3、生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER12);
        envelope.bodyOut = request;
        // c#写的应用程序必须加上这句
        envelope.dotNet = false;
        HttpTransportSE ht = new HttpTransportSE(WEB_SERVICE_URL);
        // 使用call方法调用WebService方法
        try {
            ht.call(null, envelope);
        } catch (HttpResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        try {
            //gxw- final SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            
            //gxw+-org.ksoap2.serialization.SoapPrimitive soapPrimitive =(SoapPrimitive) envelope.getResponse(); 
            //SoapObject soapObject = (SoapObject) envelope.getResponse();
             SoapObject response=(SoapObject) envelope.bodyIn;
            System.out.println(response.toString());
            final String result =  response.toString();
            if (result != null) {
                System.out.println(result.toString());
            
                return result.toString();
            }

        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }
        /*catch (SoapFault e) {
            Log.e("----发生错误---", e.getMessage());
            e.printStackTrace();
        }*/
        return null;
    }


}

你可能感兴趣的:(java web Service的使用)