AFNetworking没有收到响应数据

时间:2015-07-17 10:51:07

标签: ios json web-services afnetworking

我通过发布json对象使用AFNetworking来获取请求。但我收到空的价值。我使用以下代码。

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

NSDictionary *params = @ {@"email" :user_name, @"password" :pass_word };
[manager GET:HOST_URL parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"JSON: %@", responseObject);
    NSDictionary *weather = (NSDictionary *)responseObject;

}
      failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
     NSLog(@"opstr%@",operation.responseString);
 }];

我应该使用什么方法。如果我使用POST,我收到错误,说“操作无法完成。(Cocoa error 3840。)”(JSON文本没有以数组或对象和选项开头,以允许未设置片段。)

当我使用GET时,我的回答是空的。我不确定我错在哪里。

提前致谢。

1 个答案:

答案 0 :(得分:0)

这意味着您的请求响应不是有效的json。您可以更正Web服务以返回正确的json。或者删除以下行

manager.requestSerializer = [AFJSONRequestSerializer serializer];

因为它正在将响应转换为json。

要允许片段,请将代码更改为以下

[manager GET:HOST_URL parameters:params
  success:^(AFHTTPRequestOperation *operation, id responseObject)
{
 id json = [NSJSONSerialization JSONObjectWithData:responseObject
                                            options:NSJSONReadingAllowFragments
                                              error:&deserializingError];
NSLog(@"JSON: %@", responseObject);
NSDictionary *weather = (NSDictionary *)responseObject;

}
  failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
 NSLog(@"Error: %@", error);
 NSLog(@"opstr%@",operation.responseString);
}];