第一種:返回XMLjava
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「; apache
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);
}
json
第二種:返回字符串數組
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;
} app
// 調用編碼
Stirng url=xxx.asmx/XXX;url
string para="";spa
sendPost(url,para);.net