1. 程式人生 > >android使用ksoap2解析webservice的值為anyType{}格式的解決辦法

android使用ksoap2解析webservice的值為anyType{}格式的解決辦法

    遇到的問題,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