如何使用.asmx webservice重新调整的json在android中的列表中显示数据 [英] How to show data in list in android using json retuned by .asmx webservice
本文介绍了如何使用.asmx webservice重新调整的json在android中的列表中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的网络服务
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Web.Configuration;
[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 允许此Web服务为使用ASP.NET AJAX从脚本调用,取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class 服务:System.Web.Services.WebService
{
List< wilaya> Wilayalist = new 列表< wilaya>();
列表< moughata> Moughataalist = new List< moughata>();
List< commune> Communelist = new List< commune>();
public 服务(){
// 如果使用设计的组件,则取消注释以下行
// InitializeComponent ();
}
[WebMethod]
public string HelloWorld(){
return Hello World;
}
[WebMethod]
public 列表< wilaya> GetWilaya()
{
// 获取连接字符串
string conStr = WebConfigurationManager.ConnectionStrings [ SQLDbConnection 跨度>]的ConnectionString。
DataTable dt = new DataTable();
SqlDataReader dr = null ;
// 创建SQL连接
using (SqlConnection conn = new SqlConnection(conStr))
{
/ / 创建插入语句
string sql = string .Format( @ 从[WilayaMast]中选择*);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null ;
}
int countRow = dt.Rows.Count;
foreach (DataRow drEmp in dt.Rows)
{
Wilaya objwilaya = new Wilaya();
objwilaya.WCode = Convert.ToInt32(drEmp [ Wcode]。ToString() );
objwilaya.WName = drEmp [ WName]。ToString();
objwilaya.WID = drEmp [ WID]。ToString();
Wilayalist.Add(objwilaya);
}
返回 Wilayalist;
}
[WebMethod]
public List< commune> GetCommune()
{
// 获取连接字符串
string conStr = WebConfigurationManager.ConnectionStrings [ SQLDbConnection 跨度>]的ConnectionString。
DataTable dt = new DataTable();
SqlDataReader dr = null ;
// 创建SQL连接
using (SqlConnection conn = new SqlConnection(conStr))
{
/ / 创建插入语句
string sql = string .Format( @ 从[CommuneMast]中选择*);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null ;
}
int countRow = dt.Rows.Count;
foreach (DataRow drcommune in dt.Rows)
{
Commune objcommune = new Commune();
objcommune.CCode = Convert.ToInt32(drcommune [ CCode]。ToString() );
objcommune.CName = drcommune [ CName]。ToString();
objcommune.CID = drcommune [ CID]。ToString();
objcommune.MCode = Convert.ToInt32(drcommune [ MCode]);
Communelist.Add(objcommune);
}
return Communelist;
}
[WebMethod]
public 列表< moughata> GetMoughata()
{
// 获取连接字符串
string conStr = WebConfigurationManager.ConnectionStrings [ SQLDbConnection 跨度>]的ConnectionString。
DataTable dt = new DataTable();
SqlDataReader dr = null ;
// 创建SQL连接
using (SqlConnection conn = new SqlConnection(conStr))
{
/ / 创建插入语句
string sql = string .Format( @ 从[MoughataaMast]中选择*);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null ;
}
int countRow = dt.Rows.Count;
foreach (DataRow drmoughata in dt.Rows)
{
Moughata objmoughata = new Moughata();
objmoughata.MCode = Convert.ToInt32(drmoughata [ MCode]。ToString() );
objmoughata.MID = drmoughata [ MID]。ToString();
objmoughata.WCode = Convert.ToInt32(drmoughata [<跨度类= 代码串> 跨度> <跨度类= 代码串 > WCode跨度>]。的ToString() );
objmoughata.MName = drmoughata [ MName]。ToString();
Moughataalist.Add(objmoughata);
}
return Moughataalist;
}
}
我想将此方法返回的数据显示到android中的列表中。
解决方案
两件事。
1)更改您的Web服务代码,以便您的WebMethod
将以JSON格式返回数据。
http://stackoverflow.com/questions/19563641/how-to-get-json-response-from-a-3-5-asmx-web-service
2)然后在Android应用程序中调用您的SOAP Web服务,如下所示。
从最简单的Android应用程序调用ASP.NET Webservice(ASMX)
This is my web service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
List<wilaya> Wilayalist = new List<wilaya>();
List<moughata> Moughataalist = new List<moughata>();
List<commune> Communelist = new List<commune>();
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public List<wilaya> GetWilaya()
{
// getting connection string
string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
DataTable dt = new DataTable();
SqlDataReader dr = null;
// Creating Sql Connection
using (SqlConnection conn = new SqlConnection(conStr))
{
// Creating insert statement
string sql = string.Format(@"Select * from [WilayaMast]");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null;
}
int countRow = dt.Rows.Count;
foreach (DataRow drEmp in dt.Rows)
{
Wilaya objwilaya = new Wilaya();
objwilaya.WCode = Convert.ToInt32(drEmp["Wcode"].ToString());
objwilaya.WName = drEmp["WName"].ToString();
objwilaya.WID = drEmp["WID"].ToString();
Wilayalist.Add(objwilaya);
}
return Wilayalist;
}
[WebMethod]
public List<commune> GetCommune()
{
// getting connection string
string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
DataTable dt = new DataTable();
SqlDataReader dr = null;
// Creating Sql Connection
using (SqlConnection conn = new SqlConnection(conStr))
{
// Creating insert statement
string sql = string.Format(@"Select * from [CommuneMast]");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null;
}
int countRow = dt.Rows.Count;
foreach (DataRow drcommune in dt.Rows)
{
Commune objcommune = new Commune();
objcommune.CCode = Convert.ToInt32(drcommune["CCode"].ToString());
objcommune.CName = drcommune["CName"].ToString();
objcommune.CID = drcommune["CID"].ToString();
objcommune.MCode = Convert.ToInt32(drcommune["MCode"]);
Communelist.Add(objcommune);
}
return Communelist;
}
[WebMethod]
public List<moughata> GetMoughata()
{
// getting connection string
string conStr = WebConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
DataTable dt = new DataTable();
SqlDataReader dr = null;
// Creating Sql Connection
using (SqlConnection conn = new SqlConnection(conStr))
{
// Creating insert statement
string sql = string.Format(@"Select * from [MoughataaMast]");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
cmd = null;
}
int countRow = dt.Rows.Count;
foreach (DataRow drmoughata in dt.Rows)
{
Moughata objmoughata = new Moughata();
objmoughata.MCode = Convert.ToInt32(drmoughata["MCode"].ToString());
objmoughata.MID = drmoughata["MID"].ToString();
objmoughata.WCode = Convert.ToInt32(drmoughata["WCode"].ToString());
objmoughata.MName = drmoughata["MName"].ToString();
Moughataalist.Add(objmoughata);
}
return Moughataalist;
}
}
I want to show data returned by this method into a list in android.
解决方案
Two things.
1) Change your Web Service code so that yourWebMethod
will return data in JSON format.
http://stackoverflow.com/questions/19563641/how-to-get-json-response-from-a-3-5-asmx-web-service
2) Then call your SOAP Web service in Android application as below.
Calling ASP.NET Webservice (ASMX) from an Android Application, the Simplest Way
这篇关于如何使用.asmx webservice重新调整的json在android中的列表中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文