[color=red]1.activity[/color]
[color=red]2.cxf中的service类[/color]
package com.first;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends Activity {
private Button button =null ;
private TextView text = null ;
private EditText editText = null ;
private Handler handler = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
button = (Button)findViewById(R.id.buttonId);
text = (TextView)findViewById(R.id.firstText);
editText = (EditText)findViewById(R.id.queryId);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
text.setText("总数为:"+msg.getData().get("count").toString());
}
};
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Ksoap2Webservice(editText.getText().toString())).start();
text.setText("等待中");
}
});
}
class Ksoap2Webservice implements Runnable{
private String id ;
public Ksoap2Webservice(String id){
this.id = id ;
}
@Override
public void run() {
SoapObject object = getRemoteInfo(id) ;
if(object != null){
Message message = handler.obtainMessage();
Bundle bu = new Bundle();
bu.putString("count", object.getProperty(0).toString());
message.setData(bu);
message.sendToTarget();
}
}
/**
* @param 远程调用
*/
private SoapObject getRemoteInfo(String id) {
// 命名空间
String nameSpace = "http://webservice.com/";
// 调用的方法名称
String methodName = "sayHello";
// EndPoint
String endPoint = "http://192.168.1.192:8080/RestFulServer/services/xxWebService?wsdl";
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的参数
rpc.addProperty("id", id);
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = false; //true是网络 false是java
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
SoapObject object = null ;
try {
// 调用WebService
transport.call(null, envelope);
// 获取返回的数据
object = (SoapObject) envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
}
return object ;
}
}
}
[color=red]2.cxf中的service类[/color]
package com.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.webservice.bo.TestBo;
@WebService(targetNamespace = "http://webservice.com/")
@SOAPBinding(style = Style.RPC)
public interface FirstCxfService {
public int sayHello(@WebParam(name = "id") int id);
public TestBo getTestBo(@WebParam(name = "name") String name);
public String getNameByObject(TestBo testBo);
}