【发布时间】:2014-07-02 11:17:46
【问题描述】:
我正在尝试使用 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) request:(NSString *)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。
有人可以帮我解决这个问题吗?
【问题讨论】:
-
您是否设置过
AFHTTPRequestOperationManager实例的requestSerializer属性? -
不,我需要这样做吗?
-
在您的失败块中,尝试使用
operation.responseString或operation.responseData检查响应的原始内容。
标签: ios objective-c json ios7 afnetworking-2