JAVA 调用webservice不同返回值类型的方法
第一种:返回XML
public void XXX(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String limitStr = URLDecoder.decode(request.getParameter("limit"),"utf-8");
String offsetStr = URLDecoder.decode(request.getParameter("offset"),"utf-8");
String projectNo = URLDecoder.decode(request.getParameter("projectNo"),"utf-8");
if("null".equals(projectNo))
projectNo="";
int limit= Integer.parseInt(limitStr);
int offset =Integer.parseInt(offsetStr);
String start=String.valueOf((limit-1)*offset+1);
String end=String.valueOf(limit*offset);
//int start=(limit-1)*offset+1;
//int end=limit*offset;
// HttpClientUtil httpClientUtil = new HttpClientUtil();
// Map<String, String> createMap = new HashMap<String, String>();
// createMap.put("method", "GetAdviceData");
// createMap.put("start", start);
// createMap.put("end", end);
// createMap.put("projectNo", "");
//
// String returnJson = httpClientUtil.doPost(adviceDataUrl, createMap,
// "UTF-8");
// responseJson.writeJsonToResponse(returnJson,response);
String endpoint = ”XXX.asmx“;
String targetNamespace = "http://tempuri.org/";
//所调用接口的方法method
String method = "XXX";
// 创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(targetNamespace, method));
//变量最好只是用String类型,其他类型会报错
call.addParameter(new QName(targetNamespace, "projectNo"),
Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(targetNamespace, "start"),
Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(targetNamespace, "end"),
Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
//call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
call.setReturnType( Constants.XSD_STRING);
call.setEncodingStyle(endpoint);
call.setSOAPActionURI(targetNamespace +method);
String jsonString = (String) call.invoke(new Object[] { projectNo,start,end }); //此处为数组,有几个变量传几个变量
JSONObject obj=JSONObject.fromObject(jsonString);
String obj1= obj.getString("Table");
String cnt="0";
if(obj.has("Table1"))
{
String obj2=obj.getString("Table1");
JSONObject objcnt=JSONObject.fromObject(obj2);
cnt=objcnt.getString("number");
}
String json="{\"rows\":"+obj1+","+"\"total\":"+cnt+"}";
responseJson.writeJsonToResponse(json,response);
}
第二种:返回字符串
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
// out = new PrintWriter(conn.getOutputStream());
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"UTF-8"));//流的编码格式
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
// 调用
Stirng url=xxx.asmx/XXX;
string para="";
sendPost(url,para);