2012-07-01 13 views
6

Forse il metodo sta tornando come dovrebbe, ma io fondamentalmente appena fatto un metodo di prova che assomiglia a questoCreazione di un servizio Web ASP.net che restituisce JSON invece di XML

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string TestJSON() 
    { 
     var location = new Location[2]; 
     location[0] = new Location(); 
     location[0].Latitute = "19"; 
     location[0].Longitude = "27"; 
     location[1] = new Location(); 
     location[1].Latitute = "-81.9"; 
     location[1].Longitude = "28"; 

     return new JavaScriptSerializer().Serialize(location); 
    } 

quando mi ha colpito questo dal mio Android applicazione ottengo un'eccezione come questo

Value <?xml of type java.lang.String cannot be converted to JSONArray 

ho pensato che questo metodo potrebbe restituire JSON solo dritto, ma questo è ciò che il metodo di servizio web restituisce

<?xml version="1.0" encoding="utf-8"?> 
<string xmlns="http://tempuri.org/">[{"Latitute":"19","Longitude":"27"},{"Latitute":"-81.9","Longitude":"28"}]</string> 

Supponiamo che sia così? C'è un modo per rimuovere la roba XML fuori dal JSON? Non sono sicuro di quello che devo fare nel mio webservice per renderlo restituire il corretto formato dei dati

codice utilizzando su Android per chiamare il servizio Web

public String readWebService(String method) 
{ 
    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet("http://myserver.com/WebService.asmx/" + method); 


    Log.d(main.class.toString(), "Created HttpGet Request object"); 

    try 
    { 
     HttpResponse response = client.execute(httpGet); 
     Log.d(main.class.toString(), "Created HTTPResponse object"); 
     StatusLine statusLine = response.getStatusLine(); 
     Log.d(main.class.toString(), "Got Status Line"); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 

      return builder.toString(); 
     } else { 
      Log.e(main.class.toString(), "Failed to contact Web Service: Status Code: " + statusCode); 
     } 
    } 
    catch (ClientProtocolException e) { 
     Log.e(main.class.toString(), "ClientProtocolException hit"); 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     Log.e(main.class.toString(), "IOException hit"); 
     e.printStackTrace(); 
    } 
    catch (Exception e) { 
     Log.e(main.class.toString(), "General Exception hit"); 
    } 

    return "WebService call failed";  
} 

quindi vorrei chiamare quel metodo da qualche parte nel codice come

try { 
    JSONArray jsonArray = new JSONArray(readWebService("TestJSON")); 
    Log.i(main.class.toString(), "Number of entries " + jsonArray.length()); 
     .... 
} 
... 
+0

Come chiami questo da Android? Stai specificando un contentype in quella richiesta? –

+0

Non lo ero ma ho appena provato ad aggiungere httpGet.setHeader ("Content-Type", "application/json"); quando aggiungo questo il webservice restituisce un codice di stato di errore di 500 server. Aggiungerò la mia domanda per includere il codice Android che sto usando per chiamare il metodo di servizio web –

+0

Sembra che qualcun altro abbia avuto un problema simile a quello che ho perso durante la mia ampia ricerca (5 min googling) http://stackoverflow.com/questions/2058454/asp-net-webservice-is-wrapping-my-json-reponse-with-xml-tags? rq = 1 ... a quanto pare funziona se uso un POST invece di un get con il tipo di contenuto impostato alla domanda/JSON –

risposta

Problemi correlati