日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java调用webservice几种方式

發(fā)布時(shí)間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用webservice几种方式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一種:

android客戶(hù)端調(diào)用

傳進(jìn)參數(shù)名與參數(shù)值調(diào)用方式

package androidWsTest;


import java.io.IOException;
import java.util.Map;
import java.util.Properties;


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


import com.alibaba.fastjson.JSONObject;
public class WebserviceUtil {
private String namespace="http://webservice.zgjkw.cn/";
private String webserviceUrl="http://localhost:8080/af/webservice/";
Properties properties;

public WebserviceUtil(){
}






/**
*?
* @param namespace
* @param wsdlUrl example "mct?wsdl"
* @param methodName
* @param mehodParamName
* @param methodParam
* @return
*/
? ? public String getWebserviceResult(String wsdlUrl,String methodName,Map<String,Object> mapParams) { ?
? ? ? ? SoapObject rpc = new SoapObject(namespace, methodName);
? ? ? ??
? ? ? ?// mapParams
? ? ? ??
? ? ? ??
? ? ? ? for (Map.Entry<String, Object> entry : mapParams.entrySet()) {
? ? ? ? ? rpc.addProperty(entry.getKey().toString(), entry.getValue());
? ? ? ? }
? ? ? ??
? ? ? ?
? ? ? ??
? ? ? ? SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); ?
??
? ? ? ? envelope.setOutputSoapObject(rpc);
??
? ? ? ? HttpTransportSE transport = new HttpTransportSE(wsdlUrl);
? ? ? ??
? ? ? ? Object response = "null";
? ? ? ? String bakRst;
? ? ? ? try {
transport.call(null,envelope);
response = envelope.getResponse();
bakRst=response.toString();
} catch (IOException e) {
//Log.e("test_error", "IOException");
// Log.e("test_error", e.getMessage());
JSONObject retObj=new JSONObject();
retObj.put("success", false);
retObj.put("wserror", true);
bakRst=retObj.toJSONString();

} catch (XmlPullParserException e) {
//Log.e("test_error", "XmlPullParserException");
//Log.e("test_error", e.getMessage());
JSONObject retObj=new JSONObject();
retObj.put("success", false);
retObj.put("wserror", true);
bakRst=retObj.toJSONString();
}
??
? ? ? ? return bakRst;
? ? }

/**
*?
* @param namespace
* @param wsdlUrl example "mct?wsdl"
* @param methodName
* @param mehodParamName
* @param methodParam
* @return
*/
? ? public String getWebserviceResult(String wsdlUrl,String methodName,String mehodParamName,String methodParam) { ?
? ? ? ? SoapObject rpc = new SoapObject(namespace, methodName);
? ? ? ??
? ? ? ??
? ? ? ? rpc.addProperty(mehodParamName, methodParam);
? ? ? ??
? ? ? ? SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); ?
??
? ? ? ? envelope.setOutputSoapObject(rpc);
??
? ? ? ? HttpTransportSE transport = new HttpTransportSE(webserviceUrl+wsdlUrl);
? ? ? ??
? ? ? ? Object response = "null";
? ? ? ? String bakRst;
? ? ? ? try {
transport.call(null,envelope);
response = envelope.getResponse();
bakRst=response.toString();
} catch (IOException e) {
//Log.e("test_error", "IOException");
JSONObject retObj=new JSONObject();
retObj.put("success", false);
retObj.put("wserror", true);
bakRst=retObj.toJSONString();

} catch (XmlPullParserException e) {
//Log.e("test_error", "XmlPullParserException");
JSONObject retObj=new JSONObject();
retObj.put("success", false);
retObj.put("wserror", true);
bakRst=retObj.toJSONString();
}
??
? ? ? ? return bakRst;
? ? }
}
調(diào)用:

package androidWsTest;


import java.util.HashMap;
import java.util.Map;


public class Test {


public static void main(String[] args) {
WebserviceUtil webserviceUtil=new WebserviceUtil();
Map<String, Object> mapParams=new HashMap<String, Object>();
mapParams.put("arg0", "崔雪峰");
String msg=webserviceUtil.getWebserviceResult("http://localhost:8080/af/webservice/hello?wsdl", "hello", mapParams);
System.out.println(msg);
}
}


第二種:

通過(guò)XFire的進(jìn)行調(diào)用

package test;


import java.net.MalformedURLException;
import java.net.URL;


import org.codehaus.xfire.client.Client;


public class XfireClient {


/**
* @param args
*/
public static void main(String[] args) {
String wsUrlString = "http://localhost:8080/af/webservice/hello?wsdl";
// TODO Auto-generated method stub
try {
Client client = new Client(new URL(wsUrlString));//http://sw.zgjkw.cn/webservice/hello?wsdl
String reqString = "ww";
Object[] resObjects = client.invoke("hello", new Object[] {
"hehe"});
System.out.println(resObjects[0]);


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}
第三種:

通過(guò)axis2的調(diào)用方式

package test;


import java.io.UnsupportedEncodingException;


import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


import com.bsoft.common.HospitalServletBean;
import com.bsoft.common.json.ObjectJSonUtil;


public class RPCClient2
{
? public static void testGetRecord()
? ? throws AxisFault, UnsupportedEncodingException
? {
? ? RPCServiceClient serviceClient = new RPCServiceClient();
? ? Options options = serviceClient.getOptions();
? ? EndpointReference targetEPR = new EndpointReference(
? ? ? "http://127.0.0.1:8088/webserviceframe/services/comutilServiceImpl?wsdl");
? ?
? ? options.setTo(targetEPR);?
? ? Object[] opAddEntryArgs = {"{gnbh:'H1020101'}"};
? ? Class[] classes = {String.class };
? ? QName opAddEntry = new QName("http://service", "hospital");
? ? String result = (String)serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
? ? System.out.println(result);
? }
? /**
? ?*?
? ?* @param args
? ?* @throws Exception
? ?*/
? public static void main(String[] args)
? ? throws Exception
? {
? ? ? ?testGetRecord();
? }
}

第四種:

通過(guò)cxf客戶(hù)端調(diào)用

package cn.zgjkw.aplipay.test;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;




public class CxfClient {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsDynamicClientFactory ?factory =JaxWsDynamicClientFactory.newInstance();
? ?Client client =factory.createClient("http://localhost:8080/af/webservice/hello?wsdl");
? ?try {
? ? Object[] obj =client.invoke("hello","cuixuefeng");
? System.out.println("resp:"+obj[0]);
} catch (Exception e) {
?// TODO Auto-generated catch block
?e.printStackTrace();
}
}


}

總結(jié)

以上是生活随笔為你收集整理的java调用webservice几种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。