android ksoap2调用cxf webservice

来源:互联网 发布:学vb必看书 编辑:程序博客网 时间:2024/05/17 07:59
1.activity 

Java代码  收藏代码
  1. package com.first;  
  2.   
  3. import org.ksoap2.SoapEnvelope;  
  4. import org.ksoap2.serialization.SoapObject;  
  5. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  6. import org.ksoap2.transport.HttpTransportSE;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16. import android.widget.TextView;  
  17.   
  18. public class FirstActivity extends Activity {  
  19.       
  20.     private Button button =null ;  
  21.     private TextView text = null ;  
  22.     private EditText editText = null ;  
  23.     private Handler handler = null ;  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.first_layout);  
  28.           
  29.         button = (Button)findViewById(R.id.buttonId);  
  30.         text = (TextView)findViewById(R.id.firstText);  
  31.         editText = (EditText)findViewById(R.id.queryId);  
  32.         handler = new Handler(){  
  33.             @Override  
  34.             public void handleMessage(Message msg) {  
  35.                 text.setText("总数为:"+msg.getData().get("count").toString());  
  36.             }  
  37.         };  
  38.         button.setOnClickListener(new OnClickListener() {  
  39.             @Override  
  40.             public void onClick(View v) {  
  41.                 new Thread(new Ksoap2Webservice(editText.getText().toString())).start();  
  42.                 text.setText("等待中");  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47.     class Ksoap2Webservice implements Runnable{  
  48.           
  49.         private String id ;  
  50.         public Ksoap2Webservice(String id){  
  51.             this.id = id ;  
  52.         }  
  53.         @Override  
  54.         public void run() {  
  55.             SoapObject object = getRemoteInfo(id) ;  
  56.             if(object != null){  
  57.                 Message message = handler.obtainMessage();  
  58.                 Bundle bu = new Bundle();  
  59.                 bu.putString("count", object.getProperty(0).toString());  
  60.                 message.setData(bu);  
  61.                 message.sendToTarget();  
  62.             }  
  63.         }  
  64.   
  65.         /** 
  66.          * @param 远程调用 
  67.          */  
  68.         private SoapObject getRemoteInfo(String id) {    
  69.             // 命名空间    
  70.             String nameSpace = "http://webservice.com/";    
  71.             // 调用的方法名称    
  72.             String methodName = "sayHello";    
  73.             // EndPoint    
  74.             String endPoint = "http://192.168.1.192:8080/RestFulServer/services/xxWebService?wsdl";    
  75.         
  76.             // 指定WebService的命名空间和调用的方法名    
  77.             SoapObject rpc = new SoapObject(nameSpace, methodName);    
  78.         
  79.             // 设置需调用WebService接口需要传入的参数  
  80.             rpc.addProperty("id", id);    
  81.               
  82.             // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本    
  83.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);    
  84.               
  85.             envelope.bodyOut = rpc;    
  86.             // 设置是否调用的是dotNet开发的WebService    
  87.             envelope.dotNet = false;  //true是网络 false是java  
  88.             envelope.setOutputSoapObject(rpc);  
  89.               
  90.             HttpTransportSE transport = new HttpTransportSE(endPoint);  
  91.             SoapObject object = null ;  
  92.             try {    
  93.                 // 调用WebService    
  94.                 transport.call(null, envelope);    
  95.                 // 获取返回的数据    
  96.                 object = (SoapObject) envelope.bodyIn;    
  97.             } catch (Exception e) {    
  98.                 e.printStackTrace();    
  99.             }    
  100.         
  101.            return object ;  
  102.         }     
  103.     }  
  104. }  


2.cxf中的service类 

Java代码  收藏代码
  1. package com.webservice;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5. import javax.jws.soap.SOAPBinding;  
  6. import javax.jws.soap.SOAPBinding.Style;  
  7.   
  8. import com.webservice.bo.TestBo;  
  9.   
  10. @WebService(targetNamespace = "http://webservice.com/")  
  11. @SOAPBinding(style = Style.RPC)  
  12. public interface FirstCxfService {  
  13.     public int sayHello(@WebParam(name = "id"int id);  
  14.       
  15.     public TestBo getTestBo(@WebParam(name = "name") String name);  
  16.       
  17.     public String getNameByObject(TestBo testBo);  
  18. }  
0 0
原创粉丝点击