【问题标题】:How to ios connect web service and get json data?如何 ios 连接 Web 服务并获取 json 数据?
【发布时间】:2014-02-07 08:31:43
【问题描述】:

当我连接到返回 Json 字符串的 Web 服务时,它会像 XML 一样返回它们:

2014-02-07 00:17:15.673 Test[791:70b] <\?xml version="1.0" encoding="utf-8"?>
<\string xmlns="MBSCafe.Service.ServiceManager">{"query": [{"ID": "01","Name": "food"}]}<\/string>

相反,我想要这个:

{"query": [{"ID": "01","Name": "food"}]}

这是我的部分代码:

NSString *urlString = @"http://mbsserver/MBSCafeService/ServiceManager.asmx/GetItemTypeList";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Accept"];
NSString *myRequestString = @"id="; // Attention HERE!!!!
myRequestString = [myRequestString stringByAppendingString:@"01"];
NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
[request setHTTPBody: requestData];

这有什么问题?

【问题讨论】:

  • 您检查 WebService 的 json 响应,如果在任何情况下都不可能,则调用 webservice 并使用 XMLReader 将 xml 转换为 json。

标签: iphone web-services ios7 xcode5


【解决方案1】:

确保服务器返回的响应是json格式,可以通过fidder或者其他火狐、Chrome插件查看

【讨论】:

  • 当我在 android 中调用这个 web 服务时,我可以得到 json 对象,但在 iOS 中它像 xml 一样返回。
【解决方案2】:

使用名为“HTTP resource test”的 firefox 插件测试您的网络服务,在此处指定您的参数并查看您得到的响应。你可以使用firefox-&gt;tools-&gt;HTTP resource test找到这个插件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多