2016-05-02 10 views
0

einfach möchte ich json als Ergebnis von .net Webservice
zu verwenden, in android appwie json als Ergebnis von .net Web-Service erhalten

meine Web-Methode ist ... erhalten

 [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(); 

      } 

     } 

meine Web-Service chaotisch - Weder es 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> 

Der große Durchbruch ist nicht json noch xml war i in android bekam Httprequest wie diese alle

<HTML></HTML> 

führen, was ich von .net webservice wie dieser gerade json brauchen
https://api.github.com/user/3bdoelnaggar

+0

Ich verstehe nicht, warum Sie benötigen json Objekt zu kontrahieren – techspider

+0

mit JsonWriter Sie jede zurückkehren JSon als Antwort markieren und markieren und es wird sich um alles kümmern; keine Notwendigkeit, JSON-Zeichenkette manuell vorzubereiten – techspider

+0

sieh dir ein Beispiel hier http://www.codeproject.com/Questions/624676/JSON-Web-service-in-asp-net – techspider

Antwort

0

Verwenden Sie die folgenden. Dies funktioniert für alle HTTP-Aufrufe. Es erhält eine Zeichenfolge aus der Tabelle und kehrt zurück wie es ist. .NET konvertiert es in JSON. Siehe Abschnitt Antwortformat.

[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;  
    } 
0

ich fand die Antwort das Problem in

ist
return sb.ToString(); 

i

Context.Response.Write(TheSerializer.Serialize(oBoCityList)); 

vollständigen Code schreiben annehmen muss als folloing

 [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() { } 
     } 
0

Nutzen zu sein thi s als Beispiel !!

[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)); 
} 

Nun, wenn Sie die Web-Methode, um diese Ajax-Aufruf verwenden erstellt haben Daten aus Web-Service zu holen

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); 
    } 
});