2012-07-01 3 views
6

어쩌면 방법이 반환 어떻게해야하지만, 기본적으로 내가 그냥 내 안드로이드에서이 충돌 할 때이대신 XML의 JSON을 반환하는 ASP.net WebService를 만들기

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

과 같은 시험 방법을 만들어 응용 프로그램 나는이 방법이 단지 바로 JSON을 반환 생각이

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

같은 예외가 있지만, 이것은 웹 서비스 메소드가 리턴 무엇

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

이렇게 되겠습니까? JSON 밖에있는 XML 항목을 제거하는 방법이 있습니까? 나는 내가 어딘가에서 그 방법을 부를 것이다 나는 그것이 WebService에 다음

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

에게 전화 안드로이드에 사용 된 데이터

코드의 올바른 형식을 반환하기 위해 내 웹 서비스에 무엇을해야 확실하지 않다 코드와 같은

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

어떻게 안드로이드에서 이것을 부르시겠습니까? 해당 요청에 콘텐츠 유형을 지정하고 있습니까? –

+0

나는 단지 httpGet.setHeader ("Content-Type", "application/json")를 추가하려고 시도하지 않았다. 이 코드를 추가하면 webservice는 500 개의 서버 오류 상태 코드를 반환합니다. 웹 서비스 메서드를 호출하는 데 사용하는 안드로이드 코드를 포함하도록 내 질문을 업데이트하겠습니다. –

+0

다른 사람들이 비슷한 문제를 겪고있는 것처럼 보입니다. 내 광범위한 연구 (5 분 인터넷 검색) 중에 놓친 http://stackoverflow.com/question/2058454/asp-net-webservice-is-wrapping-my-json-reponse-xml-tags? rq = 1 ... 분명히 콘텐츠 유형이 설정된 get 대신 POST를 사용하면 작동합니다 응용 프로그램/json으로 이동 –

답변

관련 문제