简体   繁体   中英

How to display a custom listview by retriving data from asp web service in json in android

I want to display a custom listview in android.

This is my web service to filter the products using product position.

   public class Record
   {
       public string city { get; set; }
       public string description { get; set; }
       public string mobile { get; set; }
   }

   [WebMethod]
   public string filter(int flt)
   {
       string cmdStr = "select * from add_product where prod_pos ='" + flt.ToString() + "'";
       SqlDataReader rd = con.Getdata(cmdStr);

       List<Record> records = new List<Record>();

       while (rd.Read())
       {
           records.Add(new Record()
           {
               city = rd.GetString(1).ToString(),
               description = rd.GetString(3).ToString(),
               mobile = rd.GetString(5).ToString(),
           });
       }

       var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
       string sJSON = oSerializer.Serialize(records);
       rd.Close();
       return "data"+sJSON;
   }

the output of web services when we test it on server by publishing it on server.. when we pass int flt="5"

<string>
    data[
        {"city":"Aurangabad","description":"desktops dell prize 3000","mobile":"9850888129"},      
        {"city":"Pune","description":"gg","mobile":"8600525858"}
    ]
</string>

and the following is my add_product table entries....

     [id_prod               data type int]   //  auto increment... primary key
 [city          data type varchar(50)]
     [category          data type varchar(50)]
     [description           data type varchar(MAx)]
     [prod_pos          data type int]
     [mobile        datatype varchar(20)]
     [image                 datatype text]

Thanks in advance...

what i have understand that you want to display by Binding your JSON Data in Listview.. i am asuming that you are not binding your Listview and that`s why you are not getting your display

To Bind your Json Data with List view you have to write some script something like below

 <script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "YourPage.aspx/YourWebServiceName",
data: "{}",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#<%= YourListViewID.ClientID %>").append("<tr><td>" 
  + data.d[i].city+ "</td><td>"
  + data.d[i].description+ "</td><td>"
  + data.d[i].mobile+ "</td></tr>");
}
},
error: function(result) {
alert("Error");
}
});
});
</script>

You can follow the below suggestions if you are developing your application with android native platform.

  1. To retrieve data from asp web service use "Ksoap2" library. It is a SOAP web service client library used in android native application development.

  2. Then to parse json response use JSONArray and JSONObject class from org.json.* package.

    eg. JSONArray jsArray = new JSONArray(jsonResponse);

     JSONObject jsRecObjt = null; List<Record> mRecodsLst = new ArrayList<Record>(); for (int i = 0; i < jsArray .length(); i++) { 

    jsRecObjt = jsArray.getJSONObject(i);

      String city= jsRecObjt.getString("city"); String description = jsRecObjt.getString("description"); String mobile= jsRecObjt.getString("mobile"); Record rec = new Recode(); rec.setCity(); rec.setDesc(); rec.setMobile(); mRecodsLst.add(rec); } 

    Then pass this list of Records to the custom listview for displaying it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM