android与webService的数据传输---数据表=json

android与webService的数据传输---数据表=json  

2012-03-06 09:49:20|  分类: Android |  标签:json  webservice  android  数据库  传输   |字号大中小 订阅

本文在上一篇日志的基础上进行的http://yixiong89921.blog.163.com/blog/static/1325377882012250253449/

对于android调用webservice,并返回一个table数据,现在我使用json作为数据传输的手段

Json简介:推荐看这篇http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html 

Json的规则是

1) 并列的数据之间用逗号("")分隔。

2) 映射用冒号("")表示。

3) 并列数据的集合(数组)用方括号("[]")表示。

4) 映射的集合(对象)用大括号("{}")表示。

示例:

"people": 

[

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

]

现在通过实例来说明:

我的数据表Info的定义

android与webService的数据传输---数据表=json - 雨下晴川 - 小熊梦工厂
 

代码:

C#webservice的方法

 [WebMethod]

 public String getTable(string name)

 {

            SqlConnection conn = new SqlConnection(@"Data Source=SONGSFLY-PC\SQLEXPRESS;Initial     Catalog=android;Integrated Security=True");

            conn.Open();

            SqlCommand cmd = new SqlCommand("select * from Info where name='"+name+"'",conn);

            SqlDataAdapter da=new SqlDataAdapter(cmd);

            DataSet ds=new DataSet();

            da.Fill(ds);

            

            return DataTable2Json(ds.Tables[0]);

  }

将dataTable数据构造成Json格式的字符串

public string DataTable2Json(DataTable dt)

{

       if (dt != null && dt.Rows.Count > 0)

       {

           StringBuilder jsonBuilder = new StringBuilder();

           jsonBuilder.Append("{");

           jsonBuilder.Append("\"ds\"");

           jsonBuilder.Append(":[");

           for (int i = 0; i < dt.Rows.Count; i++)

           {

               jsonBuilder.Append("{");

               for (int j = 0; j < dt.Columns.Count; j++)

               {

                    jsonBuilder.Append("\""+dt.Columns[j].ColumnName+"\"");

                    jsonBuilder.Append(":\"");

                    jsonBuilder.Append(dt.Rows[i][j].ToString());

                    jsonBuilder.Append("\",");

               }

               jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

                jsonBuilder.Append("},");

            }

            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

            jsonBuilder.Append("]");

            jsonBuilder.Append("}");

            return jsonBuilder.ToString();

        }

        else

        {

           return "{\"ds\":{}}";

        }

    }

}

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("ds");//获得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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值