• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - AFNetworking 中的 JSON 错误

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:24:25 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用 AFNetworking 库向这样的 URL 发送发布请求:

https://m.com/api/2.0/basic/sim_balance.json

这需要 URL 中的用户名和密码作为我的参数(作为 NSDictionary)。

URL 像这样返回 JSON:

{
    "valid_until": "2011-05-05 22:13:28",
    "sms": 0,
    "sms_super_on_net_max": 300,
    "voice_super_on_net": 0,
    "credits": "0.00",
    "bundles": [],
    "is_expired": true,
    "sms_super_on_net": 0,
    "voice_super_on_net_max": 3600,
    "data": 0
}

这是我的请求函数:

 +(void) requestNSString *)endpoint : (NSDictionary *) parameters
    {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager POST:endpoint parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
    }

我总是将此视为错误:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x904b7e0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

我验证了 JSON 并且服务器正在发送有效的 JSON。

有人可以帮我解决这个问题吗?



Best Answer-推荐答案


要让AFHTTPRequestOperationManager将请求体序列化为JSON,需要设置其requestSerializer的值:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:....];

如果您不设置 requestSerializer 属性,则管理器对象将使用 x-www-form-urlencoded默认序列化。

但请注意,AFHTTPRequestOperationManager 默认会处理 response 正文中的 JSON。

关于ios - AFNetworking 中的 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24529755/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap