传递的数据试用json格式
示例:
{
"people":
[
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
]
}
代码:
C#的webservice的方法
[WebMethod]
public String getTable(string name)
{
SqlConnection conn = new SqlConnection(@"Data Source=Mytest-PC\SQLEXPRESS;Initial Catalog=android;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from peolpes where name='"+name+"'",conn);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
return JSONHelper. DataTableToJSON(ds.Tables[0]);
}
C#得Joson转换类
public class JSONHelper
{
/// <summary>
/// 对象转JSON
/// /// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON格式的字符串</returns>
public static string ObjectToJSON(object obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
return jss.Serialize(obj);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
}
}
/// <summary>
/// 数据表转键值对集合 www.2cto.com
/// 把DataTable转成 List集合, 存每一行
/// 集合中放的是键值对字典,存每一列
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>哈希表数组</returns>
public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName, dr[dc.ColumnName]);
}
list.Add(dic);
}
return list;
}
/// <summary>
/// 数据集转键值对数组字典
/// </summary>
/// <param name="dataSet">数据集</param>
/// <returns>键值对数组字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
{
Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();
foreach (DataTable dt in ds.Tables) result.Add(dt.TableName, DataTableToList(dt));
return result;
}
/// <summary>
/// 数据表转JSON
/// </summary>
/// <param name="dataTable">数据表</param>
/// <returns>JSON字符串</returns>
public static string DataTableToJSON(DataTable dt)
{
return ObjectToJSON(DataTableToList(dt));
}
/// <summary>
/// JSON文本转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>指定类型的对象</returns>
public static T JSONToObject<T>(string jsonText)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
return jss.Deserialize<T>(jsonText);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
}
}
/// <summary>
/// 将JSON文本转换为数据表数据
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据表字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);
}
/// <summary>
/// 将JSON文本转换成数据行
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据行的字典</returns>
public static Dictionary<string, object> DataRowFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, object>>(jsonText);
}
}
Android端代码如下
String namespace="http://tempuri.org/";
String methoName="getTable";
SoapObject soapObject=new SoapObject(namespace, methoName);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapObject.addProperty("name","songsfly");//带参数的方法调用,若调用无参数的,则无需此句
envelope.dotNet=true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTranstation=new HttpTransportSE("http://192.168.172.82/Service2.asmx?wsdl");
try {
httpTranstation.call(namespace+methoName, envelope);
Object result=envelope.getResponse()
String str=(String) result.toString();//获得请求的字符串
JSONArray jsonArray =new JSONObject(str).getJSONArray("people");//获得json字符串里名字为ds的数组,如果ds不是最外层,请通过ds的父对象来取
//JSONObject joJsonObject=new JSONObject(str).getJSONObject("dss").getJSONArray("ds");//dss是ds的外层
int count=jsonArray.length();
for(int index=0;index<count;index++){
String r=jsonArray.optJSONObject(index).getString("name").toString();//获得数组里的对象,再根据对象获得字段
strList.add(r);
}
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strList);
provinceListView.setAdapter(adapter);
}