webservice接口调用

  • Post author:
  • Post category:其他


package com.webservice;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
//import com.google.gson.Gson;
public class WebServiceTools {


/** 命名空间. */


public String NAME_SPACE = null;



/** 方法. */


public String METHOD_NAME = null;



/** WSDL文件的URL. */


public String URL = null;





public String getNAME_SPACE() {


return NAME_SPACE;


}


public void setNAME_SPACE(String nAME_SPACE) {


NAME_SPACE = nAME_SPACE;


}


public String getMETHOD_NAME() {


return METHOD_NAME;


}


public void setMETHOD_NAME(String mETHOD_NAME) {


METHOD_NAME = mETHOD_NAME;


}


public String getURL() {


return URL;


}


public void setURL(String uRL) {


URL = uRL;


}


public WebServiceTools(String namaspace, String methodname,


String url) {


super();


NAME_SPACE = namaspace;


METHOD_NAME = methodname;


URL = url;


}



@Override


public String toString() {


return “WebServiceTools [NAME_SPACE=” + NAME_SPACE + “, METHOD_NAME=”


+ METHOD_NAME + “, WSDL_URL=” + URL + “]”;


}


public String ConnectionMethodForResult(String[] values,Integer timeout) throws Exception{



// 1. 指定WebService的命名空间和调用的方法名


SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);


// 2.设置调用 WebService 方法时所需要的参数


if (values != null) {// 如果不为null


for (int i = 0; i < values.length; i++) {


request.addProperty(“arg”+i, values[i]);


}


}


// 3.实例化SoapSerializationEnvelope对象,传入SoapEnvelope.VER11,表示使用 SOAP 11


// 版本操作


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(


SoapEnvelope.VER11);


envelope.bodyOut = request;// 封装输出的SoapObject对象


envelope.dotNet = false;// 不是.NET服务器,设置为false,表示服务器端接受请求参数


envelope.setOutputSoapObject(request);// 设置要输出的 SoapObject对象


// 4.实例化HttpTransportSE对象,传入完整的URL路径


HttpTransportSE ht = new HttpTransportSE(URL, timeout);


ht.debug = true;


// 调用 WebService 端的操作方法


String soapAction = NAME_SPACE+”/”+METHOD_NAME;


ht.call(soapAction, envelope);
//


SoapObject response =  (SoapObject) envelope.bodyIn;
//


Object result = response.getProperty(“return”);//取得返回的内容


//if (result!=null) {//如果开头为错误标记



//System.out.print(result);
//


if (returnClass==null) {
//

//


}else{
//

//


System.out.println(new Gson().fromJson(result.toString(),returnClass).toString());
//

//


}


Object ob =envelope.getResponse();


String str = ob.toString();


System.out.println(str);


return str;


}




public WebServiceTools() {


super();


// TODO Auto-generated constructor stub


}


public String ConnMethod(String[] values,Integer timeout,String namespace,String methodname,String url) throws Exception{



this.setNAME_SPACE(namespace);


this.setMETHOD_NAME(methodname);


this.setURL(url);


return this.ConnectionMethodForResult(values,timeout);


}



public static void main(String[] args) throws Exception{


WebServiceTools wt = new WebServiceTools();
//


添加案件


wt.addanjian();
//


添加档案
//


wt.adddab();


}




/**


* 添加案件


* @throws Exception


*


* */


public String addanjian() throws Exception{





String ids = this.getPrimarykey();


SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);


String values[] = {


“{‘onlyid’:”,’ydwz’:’用地位置’,’mj’:’200′}”,




“{‘onlyid’:”,’dwmc’:’单位名称’}”,




“{‘onlyid’:”,’sdfyrq’:'”+sdf.format(new Date())+”‘,’fyslrq’:'”+sdf.format(new Date())+”‘,’sdgzbrq’:'”+sdf.format(new Date())+”‘,’gzbslrq’:'”+sdf.format(new Date())+”‘,’sdgarq’:'”+sdf.format(new Date())+”‘,’gaslrq’:'”+sdf.format(new Date())+”‘,’djcf’:’党纪处分’}”,




“{‘onlyid’:”,’ajly’:’案件来源’,’nfwplx’:’年份卫片类型(变更/执法)’,’tbh’:’图斑号’,’labh’:’立案号’,’zj’:’镇街’,’zbhx’:’坐标红线’,’lasj’:'”+sdf.format(new Date())+”‘,’cfjdsrq’:'”+sdf.format(new Date())+”‘,’jasj’:'”+sdf.format(new Date())+”‘}”,




“[{‘isdel’:’0′,’cffs’:’拆除’,’mj’:’200′,’je’:’200′},{‘isdel’:’0′,’cffs’:’没收’,’mj’:’200′,’je’:’200′},{‘isdel’:’0′,’cffs’:’罚款’,’mj’:200,’je’:200}]”,ids




};



return this.ConnMethod(values, 30000,


“addanjianqingkuang”,





}



/**


*添加档案


* @throws Exception


*


* */


public String adddab() throws Exception{


String ids = this.getPrimarykey();



SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);


String values[] = {


“{‘dah’:’123′,’qjr’:’g’,’qjsj’:'”+sdf.format(new Date())+”‘,’cjr’:’g’,’cjsj’:'”+sdf.format(new Date())+”‘,’smr’:’g’,’smsj’:'”+sdf.format(new Date())+”‘,’scsj’:'”+sdf.format(new Date())+”‘,’scr’:’g’,’smjcfwz’:’/gg/hh’}”,ids




};



return this.ConnMethod(values, 30000,


“adddab”,
}


/**


* 获取id主键


*


* */


public String getPrimarykey() throws Exception{



SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);


String values[] = {};



String str = this.ConnMethod(values, 30000,


“getPrimarykey”,



return str;


}
}