我尝试了几个选项设置responseSerializer
至JSON
但我无法将responseObject转换为NSDictionary
。问题是响应我得到是NSData
AFNetworking GET请求 - 无法将响应对象转换为NSDictionary
NSString *baseURLString = @"Your URL?";
NSString *str = @"adult=false&gender=male";
NSString *url = [NSString stringWithFormat:@"%@%@",baseURLString,str];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do whatever you'd like here; for example, if you want to convert
// it to a string and log it, you might do something like:
NSLog(@"responseObject %@",responseObject);
NSString *jsonString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseObject
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"json %@",json);
if (error) {
NSLog(@"%@",[error description]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这NSLog(@"%@",[error description]);
如下提示错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
(Cocoa error 3840.)" (Garbage at end.)
UserInfo=0xa7c5440 {NSDebugDescription=Garbage at end.}
但是,您正在从请求中撤回的响应对象的格式是什么? – MrBr
@MrBr - 它的NSData我可以将它转换为字符串 - jsonString –
它可能不是一个有效的JSON然后,你可以使用'NSJSONSerialization'来分析json字符串吗? –