android ksoap2调用cxf webservice

[color=red]1.activity[/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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值