我嘗試了幾個選項設置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字符串嗎? –