SoapObject解析成List<String[]>二維數(shù)組
最近和Webservice對(duì)接接口,接到需求我乃一臉懵逼啊,廢話不多說,看代碼
首先是服務(wù)器接口給的url和方法名
private static final String TRANS_URL = "http://192.168.0.16:8005/WebService.asmx";
private static final String METHED = "DownLoadDataByFloor";
private static final String NAMESPACE = "http://tempuri.org/";//默認(rèn)
NAMESPACE 可以在URL后面加上?wsdl既可以閱覽,如下圖
繼續(xù)擼代碼
private void getMesInfo(final String FloorName, String StepName, final String CurPage, final String PageSize) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100) {
if (dialogLoading != null) {
dialogLoading.closeDialog();
dialogLoading = null;
}
springView.onFinishFreshAndLoad();
springView.onFinishFreshAndLoad();
no_choose_tv.setVisibility(View.GONE);
SoapObject result = (SoapObject) msg.obj;
list.addAll(getSoapObj(result));
adapter.notifyDataSetChanged();
}else if (msg.what==101){
if (dialogLoading != null) {
dialogLoading.closeDialog();
dialogLoading = null;
}
springView.onFinishFreshAndLoad();
springView.onFinishFreshAndLoad();
no_choose_tv.setVisibility(View.GONE);
MyUtil.showAutoToast(context,"暫無(wú)更多數(shù)據(jù)");
if (list.size()==0){
no_choose_tv.setVisibility(View.VISIBLE);
no_choose_tv.setText("暫無(wú)更多數(shù)據(jù)");
}
}
}
};
Log.e("開始連接服務(wù)器", FloorName + StepName + "第" + CurPage + "頁(yè)");
final SoapObject request = new SoapObject(NAMESPACE, METHED);
request.addProperty("FloorName", FloorName);
request.addProperty("StepName", StepName);
request.addProperty("CurPage", CurPage);
request.addProperty("PageSize", PageSize);
// request.addProperty("MaxPage", "3");
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
if (CurPage.equals("1")) {
dialogLoading = new DialogLoading(context, "加載中...");
list.clear();
adapter.notifyDataSetChanged();
}
new Thread(new Runnable() {
@Override
public void run() {
HttpTransportSE ht = new HttpTransportSE(TRANS_URL);
ht.debug = true;
try {
ht.call(soapAction, envelope);
SoapObject object = (SoapObject) envelope.bodyIn;
Message msg = new Message();
if (page>1&&((SoapObject)object.getProperty(0)).getPropertyCount()<1){
msg.what = 101;
page--;
}else{
msg.what = 100;
msg.obj = object;
}
handler.sendMessage(msg);
Log.e("getMesInfo", object.toString());
} catch (Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
MyUtil.showAutoToast(context,"連接超時(shí)");
no_choose_tv.setVisibility(View.VISIBLE);
no_choose_tv.setText("連接超時(shí)\n請(qǐng)確保您的網(wǎng)絡(luò)為公司內(nèi)網(wǎng)\n點(diǎn)擊重新加載");
}
});
if (dialogLoading != null) {
dialogLoading.closeDialog();
dialogLoading = null;
}
Log.e("getMesInfo", e.getMessage().toString());
e.printStackTrace();
}
}
}).start();
}
這是筆者封裝的網(wǎng)絡(luò)請(qǐng)求,新開線程執(zhí)行網(wǎng)絡(luò)請(qǐng)求部分,然后在到handler處理更新listview和ui,其中
list.addAll(getSoapObj(result));```
中的`getSoapObj()`方法如下,花了一天時(shí)間才弄明白
private List<String[]> getSoapObj(SoapObject object){
String[] string = null;
List<String []> aaa=new ArrayList<>();
if (((SoapObject)object.getProperty(0)).getPropertyCount()>0){
for (int i=0;i<((SoapObject)object.getProperty(0)).getPropertyCount();i++){
string=new String[((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(0)).getPropertyCount()];
for (int j=0;j< ((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(i)).getPropertyCount();j++){
string[j]=((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(i)).getProperty(j).toString();
}
if (string!=null){
aaa.add(i,string);
}
}
}else {
if (page==1){
no_choose_tv.setVisibility(View.VISIBLE);
no_choose_tv.setText("暫無(wú)更多數(shù)據(jù)");
}
}
return aaa;
}
####看著代碼很冗長(zhǎng),其實(shí)SoapObject相當(dāng)于數(shù)組,只是不能直接當(dāng)成數(shù)組用,所以我們要把它的數(shù)據(jù)取出來(lái)自己弄到數(shù)組中去,在源碼中我找到一個(gè)超級(jí)好用的方法
在SoapObject的源碼中
public int getPropertyCount() {
return this.properties.size();
}
這個(gè)方法等于list.size().也就是放回SoapObject對(duì)象中當(dāng)前層的長(zhǎng)度
有了這個(gè)方法,就能對(duì)著獲得的SoapObject對(duì)象toString()之后研究它的模型構(gòu)造.比如
DownLoadDataByFloorResponse{DownLoadDataByFloorResult=anyType{ArrayOfString=anyType{string=4F DIP C線; string=DIP4F_C; string=TW.C.ITV050-G; string=4.002.1188; string=999; string=999; string=0; string=0.00%; }; ArrayOfString=anyType{string=4F DIP C線; string=DIP4F_C; string=TW.C.ITV050-J; string=4.002.1367; string=100; string=100; string=2; string=2.00%; }; ArrayOfString=anyType{string=4F DIP D線; string=DIP4F_D; string=TW.C.ITV041-H; string=4.002.1068; string=1772; string=1980; string=7; string=0.35%; }; }; MaxPage=2; }
接口人員說給我返回的List<String[]>,所以開始解析時(shí)我一直以為一定是這樣,其實(shí)不然,已經(jīng)發(fā)生了翻天覆地的變化.
然后就是一層一層的解析
`object.getProperty(0)`之后發(fā)現(xiàn)還是不能直接循環(huán)取數(shù)據(jù),所以只能把`object.getProperty(0)`之后的對(duì)象繼續(xù)轉(zhuǎn)成SaopObject繼續(xù)`getProperty()`,
由于WebService那邊返回的確實(shí)是List<String[]>,所以Android這邊在第二次`getProperty()`就能開始循環(huán)了,循環(huán)的對(duì)象就是List,第三次`getProperty()`后的循環(huán)就可以直接添加到String[]里面去了.大工搞成.
`listview.setAdapter(context,list);`
一切又回到了我喜歡樣子.