1

我尝试使用 Monotouch 通过 HttpWebRequest 对象获取 JSON 数据。它在 iPhone 模拟器中运行良好,我得到了 JSON。但是当我在设备中运行应用程序时,当调用 Web 服务时,我总是返回 XML 而不是 JSON。

从 iPhone 运行时,是否有任何特定的配置参数需要设置才能以 JSON 格式获得结果?我在 iPhone 5 和 ios 6 上运行它。

这是我的代码..

var request = HttpWebRequest.Create(String.Format (@"{0}/GetActiveProductCountAfterID/filter?minID={1}",baseUrl, lastProductNumberInDatabase));
Logger.Debug("Request URL is: " + request.RequestUri);
request.ContentType = @"application/json";  
request.Method = "GET";
try{
     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
     {
        if (response.StatusCode != HttpStatusCode.OK)
           Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            string content = reader.ReadToEnd();
........

当我在模拟器中运行时,我将内容作为整数..例如:3456

但是当我从 iPhone 上运行它时,我得到了

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">3456</int>
4

1 回答 1

0

通过将服务器端 Web 服务设置为将 json 作为默认 Web 服务来解决它。对于那些可能遇到相同问题的人,这是我必须做的以将 Json 作为默认格式(在服务器中 .Net WCF 上的 web.config 文件中)

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true"   defaultOutgoingResponseFormat="Json" />
  </webHttpEndpoint>
</standardEndpoints>

于 2012-10-13T14:32:20.627 回答