在libs下添加
ksoap2-android-assembly-3.4.0-jar-with-dependencies.jar
//异步调用Web Service,获取数据
public class JsonAsynTaskXml extends AsyncTask<Void, Void, String> {
private static final String TAG = "JsonAsynTaskXml";
//获取返回值后的回调接口
private WebServiceListenerXml lisener;
//获取数据开始到获取到数据中间时间段的转圈动画
private CustomProgressDialog progressDialog;
private String param = "";
private String arg0;
private String arg1;
private boolean iSprogressShow;
private boolean isSheep;
private Context context;
private String endPoint;
/**
* 处理新的webservice
* @param endPoint
*/
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
//设置参数
public void setParams(Map<String, String> map) {
for (String key : map.keySet()) {
param += key+"="+ map.get(key)+";";
}
}
public void setArg0(String arg0) {
this.arg0 = arg0;
}
public void setArg1(String arg1) {
this.arg1 = arg1;
}
public JsonAsynTaskXml(WebServiceListenerXml lisener, Context context,boolean iSprogressShow,boolean isSheep) {
super();
this.iSprogressShow = iSprogressShow;
this.isSheep = isSheep;
this.context = context;
//如果要实现转圈动画,就实例化CustomProgressDialog
if(iSprogressShow){
if (context != null) {
progressDialog = new CustomProgressDialog(context, R.style.progressDialog);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}
this.lisener = lisener;
}
/* public JsonAsynTaskXml(WebServiceListenerXml lisener){
super();
this.lisener = lisener;
}*/
@Override
protected void onPostExecute(String result) {
if(iSprogressShow){
if (progressDialog != null) {
progressDialog.dismiss();
}
}
LogUtils.e(TAG, result+"");
lisener.onComplete(result);
}
@Override
protected String doInBackground(Void... params) {
if(isSheep){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
创建SoapObject对象,
参数Constants.nameSpace对应WebService命名空间,
参数Constants.methodName对应WebService方法名 ··
SoapObject request = new SoapObject(Constants.nameSpace, Constants.methodName);
//调用SoapObject对象的addProperty方法来设置参数
//参数名不一定要和服务器端的参数名相同,只要参数顺序相同就可以
request.addProperty("arg0", arg0);
request.addProperty("arg1", arg1);
request.addProperty("arg2", param);
request.addProperty("arg3", "json");
//创建某一版本的SoapSerializationEnvelope对象
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER10);
//将SoapObject创建的对象,
//设置为SoapSerializationEnvelope传出的SOAP消息体
//envelope.setOutputSoapObject(request);也可以这样设置
envelope.bodyOut = request;
LogUtils.e("param", param);
String mAccounts = PrefsUtils.readPrefs(context, "mAccounts");
Element[] header = SoapHeader.getElement(mAccounts);
//封装下SoapHeader的参数名和参数值赋值给SoapSerializationEnvelope对象的headerOut属性
envelope.headerOut = header;
if(endPoint ==null){
endPoint = Constants.endPoint;
LogUtils.e("endPoint", endPoint);
}
LogUtils.e("TAG", endPoint);
//创建HttpTransportSE对象,该对象用于调用WebService操作
HttpTransportSE ts = new HttpTransportSE(endPoint,20000);
try {
//调用call方法,并以SoapSerializationEnvelope对象为参数,调用远程的Web Service
ts.call(null, envelope);
//直接调用SoapSerializationEnvelope对象的getResponse方法获取返回值
//获取返回值还可以以((SoapObject) envelope.bodyIn).getProperty(0).toString()方式获取
return envelope.getResponse().toString();
} catch (IOException e) {
e.printStackTrace();
if(iSprogressShow){
if (progressDialog != null) {
progressDialog.dismiss();
}
}
return null;
} catch (XmlPullParserException e) {
e.printStackTrace();
if(iSprogressShow){
if (progressDialog != null) {
progressDialog.dismiss();
}
}
return null;
}
};
}
//回调接口
public interface WebServiceListenerXml {
void onComplete(String result);
}
//封装SoapHeader的类
public class SoapHeader {
public static Element[] getElement(String mAccounts) {
try {
Element[] header = new Element[1];
header[0] = new Element().createElement("MySoapHeader", "MySoapHeader");
Element username = new Element().createElement("username", "username");
username.addChild(Node.TEXT, encode(mAccounts));
header[0].addChild(Node.ELEMENT, username);
/* Element pass = new Element().createElement("vcode", "vcode");
pass.addChild(Node.TEXT, encode(VcodeUtil.createVcode()));
header[0].addChild(Node.ELEMENT, pass); */
return header;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Android 异步调用websevice接口
最新推荐文章于 2022-08-04 10:27:34 发布