首先放上ksoap2的jar包:http://download.csdn.net/detail/xf396191344/7516667
遇到的问题,soapObject打印出来的值:GetGongdanResponse{GetGongdanResult=anyType{results=anyType{details=anyType{id=c252cd5d-8e1f-466f-9b1c-95d8ec55ba82; title=新增个人客户-业务功能拓扑工单; typeid=30; balances=88.38; addtime=2014/6/13 10:57:15; }; }; }; }
解决办法的核心代码:
SoapObject result = (SoapObject) envelope.bodyIn;
// 获取子节点
SoapObject soapChilds = (SoapObject) result.getProperty(0);
try {
SoapObject results = (SoapObject) soapChilds
.getProperty("results");
// 遍历获取details节点
for (int i = 0; i < results.getPropertyCount(); i++) {
SoapObject details = (SoapObject) results.getProperty(i);
String title = details.getProperty("title").toString();
String typeid = details.getProperty("typeid").toString();
String balances = details.getProperty("balances")
.toString();
String addtime = details.getProperty("addtime").toString();
projectInfo.add(new String[] { title, typeid, balances,
addtime });
}
用循环遍历的方式取出各个节点的对象(results、details、id、title、typeid、balances、addtime都相当于是一个节点,我也不知道这么命名准确么,姑且这么叫吧)
上完整代码:
package com.xf.webservicedemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* @author xf
* Android平台调用WebService(.net平台)
*
*/
public class MainActivity extends Activity {
// 显示结果的TextView
private TextView resultView;
// 查询webservice的按钮
private Button queryButton;
private Handler handler;
// 结果的值
private String resultValue;
// 存放结果的信息
private List<String[]> projectInfo = new ArrayList<String[]>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
// 返回的结果
resultView = (TextView) findViewById(R.id.result_text);
// 查询按钮
queryButton = (Button) findViewById(R.id.query_btn);
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
getInfomation();
handler.post(r);
}
});
t.start();
}
});
}
private Runnable r = new Runnable() {
@Override
public void run() {
// 将WebService返回的结果显示在TextView中
resultView.setText(resultValue);
}
};
// webservice的交互
private String getInfomation() {
String namespace = "http://tempuri.org/";
String method = "GetGongdan";
String endpoint = "http://218.25.172.121:9009/AppTest.asmx";
String soapAction = "http://tempuri.org/GetGongdan";
SoapObject rpc = new SoapObject(namespace, method);
rpc.addProperty("key", "dhccpass");
rpc.addProperty("pageSize", 1);
rpc.addProperty("pageIndex", 0);
// rpc.addProperty("userId", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endpoint);
transport.debug = true;
try {
transport.call(soapAction, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
// 获取子节点
SoapObject soapChilds = (SoapObject) result.getProperty(0);
try {
SoapObject results = (SoapObject) soapChilds
.getProperty("results");
// 遍历获取details节点
for (int i = 0; i < results.getPropertyCount(); i++) {
SoapObject details = (SoapObject) results.getProperty(i);
String title = details.getProperty("title").toString();
String typeid = details.getProperty("typeid").toString();
String balances = details.getProperty("balances")
.toString();
String addtime = details.getProperty("addtime").toString();
projectInfo.add(new String[] { title, typeid, balances,
addtime });
}
StringBuilder sb = new StringBuilder();
sb.append("id:" + projectInfo.get(0)[0] + "\n")
.append("typeid:" + projectInfo.get(0)[1] + "\n")
.append("balances:" + projectInfo.get(0)[2] + "\n")
.append("addtime:" + projectInfo.get(0)[3] + "\n");
resultValue = sb.toString();
} catch (Exception e) {
}
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return resultValue;
}
}
项目源码下载地址:
http://download.csdn.net/detail/xf396191344/7516879