根本就是我想从.net web服务中获取json 在Android应用程序中使用
我的网络方法是......
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 GetFlash : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetOneFlash()
{
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["eng_lang_tutConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand sqlCommand =
new SqlCommand(@"SELECT * FROM focus WHERE P_ID = " + HttpContext.Current.Request.QueryString["IndexOrder"], con);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);
try
{
SqlDataReader reader = sqlCommand.ExecuteReader();
reader.Read();
int fieldcount = reader.FieldCount; // count how many columns are in the row
object[] values = new object[fieldcount]; // storage for column values
reader.GetValues(values); // extract the values in each column
jsonWriter.WriteStartObject();
for (int index = 0; index < fieldcount; index++)
{
jsonWriter.WritePropertyName(reader.GetName(index)); // column name
jsonWriter.WriteValue(values[index]); // value in column
}
jsonWriter.WriteEndObject();
reader.Close();
}
catch (SqlException sqlException)
{
con.Close();
return ("No data fetched ..." + "\n" + "---------------------------");
}
finally
{
con.Close();
}
return sb.ToString();
}
}
我的网络服务变得混乱 - 既不是json也不是xml http://englishflash.somee.com/WebService/GetFlash.asmx/GetOneFlash?IndexOrder=1
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
{"rownumber":1,"P_ID":120,"F_Eword":"a.m. ","F_Aword":"صباحا ","F_Notes":"","Usage":"","F_pic":null,"F_pronounce":"https://ssl.gstatic.com/dictionary/static/sounds/de/0/a.m..mp3"}
</string>
重大突破是我在Android HTTPrequest结果中得到了这样的
<HTML></HTML>
我所需要的只是json来自.net webservice就像这样 https://api.github.com/user/3bdoelnaggar
答案 0 :(得分:0)
使用以下内容。这适用于所有HTTP调用。它从表中获取字符串并按原样返回。 .NET将其转换为JSON。请参阅响应格式部分。
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]
public static string GetDetails(int ID)
{
PDetails PDetails = new PDetails();
OrderManager oOrdManager = new OrderManager();
PDetails = oOrdManager.GetDetailInformation(ID);
return PDetails.DetailInfo;
}
答案 1 :(得分:0)
我找到了答案 问题在于
return sb.ToString();
我想写一下
Context.Response.Write(TheSerializer.Serialize(oBoCityList));
完整代码必须如下
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetCities()
{
CityList oBoCityList = new CityList() { new City { Name = "New Delhi", ID = 1 }, new City { Name = "Kanpur", ID = 2 }, new City { Name = "Gurgaon", ID = 3 } };
JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
//return TheSerializer.Serialize(oBoCityList);
Context.Response.Write(TheSerializer.Serialize(oBoCityList));
}
}
public class City
{
public City() { }
public string Name
{ get; set; }
public Int32 ID
{ get; set; }
}
public class CityList : List<City>
{
public CityList() { }
}
答案 2 :(得分:0)
以此为例!!
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getCustomers()
{
var clientes = from result in dados.clientes select result;
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Write(JsonConvert.SerializeObject(clientes));
}
现在,当您创建了Web方法时,请使用此ajax调用从Web服务中获取数据
var url = "YourURL_of_Webservice"; // example : "http://localhost:54028/webservice.asmx/getCustomers"
$.ajax({
type: "POST",
url: url,
success: function (data) {
// YOUR CODE FOR SUCCESS BODY
console.log(data)
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log(xmlHttpRequest.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});