• ksoap调用cxf webservice传递类对象参数,返回类对象


    感谢万能的互联网,废话少说,挑关键代码上了。

    服务端:

    //服务接口
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    import com.jnbus.beans.DemoBean;
    
    @WebService
    public interface JNBusWsI {
    
    	public byte[] testA(@WebParam(name="a")int pA, @WebParam(name="b")DemoBean pB);
    	public String testB(@WebParam(name="c")int pC, @WebParam(name="d")String pD);
    	
    }
    
    
    //接口实现类
    import com.jnbus.beans.DemoBean;
    
    public class JNBusWsImpl implements JNBusWsI {
    
    	public byte[] testA(int pa, DemoBean pb) {
    		return new DemoBean(pa, pb.getName() + "###").serialize();
    	}
    
    	public String testB(int pc, String pd) {
    		return "testB responsed, and the param you passed is " + pc + "-" + pd;
    	}
    
    }
    
    //测试中需要传递和返回的对象
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    
    public class DemoBean implements IBaseBean {
    	
    	private int id;
    	private String name;
    	
    	public DemoBean() {
    		
    	}
    	
    	public DemoBean(int id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    	
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public byte[] serialize() {
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		DataOutputStream dos = new DataOutputStream(baos);
    		try {
    			dos.writeInt(this.id);
    			dos.writeUTF(this.name);
    		} catch (IOException e) {
    		} finally {
    			try {
    				if (dos != null)
    					dos.close();
    				if (baos != null)
    					baos.close();
    			} catch (IOException e) {
    			}
    		}
    		
    		return baos.toByteArray();
    	}
    	
    }
    
    public interface IBaseBean {
    	public byte[] serialize();
    }
    
    
    //顺便把验证用户密码的也复制过来
    //没看到ksoap支持wsse的例子
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.soap.SOAPMessage;
    
    import org.apache.cxf.binding.soap.SoapMessage;
    import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
    import org.apache.cxf.interceptor.Fault;
    import org.apache.cxf.phase.AbstractPhaseInterceptor;
    import org.apache.cxf.phase.Phase;
    import org.w3c.dom.NodeList;
    
    public class SAuthHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    	
    	private SAAJInInterceptor saajIn = new SAAJInInterceptor();
    	private final static String NAMESPACE = "http://jnbus.com/";
    
    	public SAuthHeaderInterceptor() {
    		super(Phase.PRE_PROTOCOL);
    		getAfter().add(SAAJInInterceptor.class.getName());
    	}
    
    	public void handleMessage(SoapMessage msg) throws Fault {		
    		SOAPMessage soapMessage = getSOAPMessage(msg);
    		if (soapMessage != null) {
    			try {
    				SOAPHeader soapHeader = soapMessage.getSOAPHeader();
    				if (soapHeader != null) {
    					NodeList nodeList = soapHeader.getElementsByTagNameNS(NAMESPACE, "AuthName");
    					if (nodeList != null && nodeList.item(0) != null) {
    						String authName = nodeList.item(0).getTextContent();
    						if (authName.equals("jnbus")) {
    							nodeList = soapHeader.getElementsByTagNameNS(NAMESPACE, "AuthPassword");
    							if (nodeList != null && nodeList.item(0) != null) {
    								String authPsw = nodeList.item(0).getTextContent();
    								if (!authPsw.equals("jnbuspsw")) {
    									throw new Fault(new IllegalArgumentException("AuthPassword Wrong"));
    								}
    							} else {
    								throw new Fault(new IllegalArgumentException("You should add AuthPassword"));
    							}
    						} else {
    							throw new Fault(new IllegalArgumentException("AuthName Wrong"));
    						}
    					} else {
    						throw new Fault(new IllegalArgumentException("You should add AuthName"));
    					}
    				}
    			} catch (SOAPException e) {
    				
    			}
    		}
    	}
    	
    	private SOAPMessage getSOAPMessage(SoapMessage msg) {
    		SOAPMessage SOAPMsg = msg.getContent(SOAPMessage.class);
    		if (SOAPMsg == null) {
    			saajIn.handleMessage(msg);
    			SOAPMsg = msg.getContent(SOAPMessage.class);
    		}
    		return SOAPMsg;
    	}
    
    }
    

    android端:

    //要想把类对象当作参数传递出去,需实现KvmSerializable接口,并实现其中方法
    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.util.Hashtable;
    
    import org.ksoap2.serialization.KvmSerializable;
    import org.ksoap2.serialization.PropertyInfo;
    
    public class DemoBean implements KvmSerializable{
    	
    	private int id;
    	private String name;
    	
    	public DemoBean() {
    		
    	}
    	
    	public DemoBean(int id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public static DemoBean deserialize(byte[] data) {
    		ByteArrayInputStream bais = new ByteArrayInputStream(data);
    		DataInputStream dis = new DataInputStream(bais);
    		DemoBean demoBean = new DemoBean();
    		try {
    			demoBean.setId(dis.readInt());
    			demoBean.setName(dis.readUTF());
    		} catch (NumberFormatException e) {
    		} catch (IOException e) {
    		} finally {
    			try {
    				if (bais != null)
    					bais.close();
    				if (dis != null)
    					dis.close();
    			} catch (IOException e) {
    			}
    		}
    		return demoBean;
    	}
    
    	@Override
    	public Object getProperty(int arg0) {
    		switch (arg0) {
    		case 0:
    			return this.id;
    		case 1:
    			return this.name;
    		default:
    			break;
    		}
    		return null;
    	}
    
    	@Override
    	public int getPropertyCount() {
    		return 2;
    	}
    
    	@Override
    	public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
    		switch (arg0) {
    		case 0:
    			arg2.type = PropertyInfo.INTEGER_CLASS;
    			arg2.name = "id";
    			break;
    		case 1:
    			arg2.type = PropertyInfo.STRING_CLASS;
    			arg2.name = "name";
    			break;
    		default:
    			break;
    		}
    		
    	}
    
    	@Override
    	public void setProperty(int arg0, Object arg1) {
    		switch (arg0) {
    		case 0 :
    			id = Integer.valueOf(arg1.toString());
    			break;
    		case 1 :
    			name = arg1.toString();
    			break;
    		default:
    			break;
    		}
    	}
    }
    
    
    //
    import org.kobjects.base64.Base64;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import org.kxml2.kdom.Element;
    import org.kxml2.kdom.Node;
    
    import com.njty.beans.DemoBean;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class DemoWebServiceActivity extends Activity {
    	
        public static String NAMESPACE = "http://jnbus.com/";
        public String methodName = "testA";
        public String soapAction = NAMESPACE + methodName;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            Button btn = (Button)findViewById(R.id.btnQuery);
            btn.setOnClickListener(new View.OnClickListener() {
            	TextView tv = (TextView)findViewById(R.id.ret);
    			@Override
    			public void onClick(View v) {
    				tv.setText(getWS());
    			}
    		});
            
        }
        
        public String getWS() {
        	String ret = null;
        	try {
    			SoapObject request = new SoapObject(NAMESPACE, methodName);
    			request.addProperty("a", 420);
    			request.addProperty("b", new DemoBean(1, "ss"));
    			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    			envelope.dotNet = false;
    			envelope.setOutputSoapObject(request);
    			envelope.headerOut = new Element[2];
    			envelope.headerOut[0] = getAuthName();
    			envelope.headerOut[1] = getAuthPsw();
    			
    			HttpTransportSE ht = new HttpTransportSE("http://192.168.6.19:8008/JNBusWS");
    			ht.call(soapAction, envelope);
    			if (envelope.getResponse() != null) {
    				SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
    				ret = response.toString();
    				byte[] bytes = Base64.decode(ret);
    				DemoBean demoBean = DemoBean.deserialize(bytes);
    				ret = "Id=" + demoBean.getId() + " Name=" + demoBean.getName();
    			}
    		} catch (Exception e) {
    			ret = e.getMessage();
    		}
        	return ret;
        }
        
        private Element getAuthName() {
        	//Element header = new Element().createElement(NAMESPACE, "AuthUser");
        	Element authName = new Element().createElement(NAMESPACE, "AuthName");
        	authName.addChild(Node.TEXT, "jnbus");
        	return authName;
        }
        
        private Element getAuthPsw() {
        	Element authPsw = new Element().createElement(NAMESPACE, "AuthPassword");
        	authPsw.addChild(Node.TEXT, "jnbuspsw");
        	return authPsw;
        }
        
    }
    

      

  • 相关阅读:
    问题:使用pandas中的DataFrame写入csv文件多出一行unnamed,如何解决呢??
    使用pandas进行数据分析-pandas库介绍之DataFrame基本操作
    【USACO 2017 February Gold】Problem 3. Why Did the Cow Cross the Road III
    JZOJ【USACO 2017 December Silver】Milk Measurement
    JZOJ.2020.05.23【NOIP提高组】模拟(反思)
    PRIM+KRUSKAL
    draggable
    easyUI使用的两种方法
    MySQL 常见函数
    jdbc,hibernate,mybatis调用存储过程
  • 原文地址:https://www.cnblogs.com/jadic/p/2342925.html
Copyright © 2020-2023  润新知