AFNetworking:无法从AFHTTPRequestOperation获取响应字符串

Her*_*ber 5 networking nsurlrequest ios afnetworking

任何人?):我遇到的问题让我在过去的2个小时里刮过头脑,这很可能是一件非常简单的愚蠢的事情.当我从操作@ AFNetworking调用响应字符串时,我继续收到构建错误...就像没有这样的属性....

请看一下我的代码并解释一下我搞砸了这次:p ..谢谢:)


NSDictionary* paramDict = [NSDictionary dictionaryWithObjectsAndKeys:WebServicemd5Value, WebSermd5Variable, nil]
;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:webServiceURL]];

[httpClient defaultValueForHeader:@"Accept"];

[httpClient postPath:@"method" parameters:paramDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response data: %@", responseObject);
    NSLog(@"Reponse String: %@", operation);
Run Code Online (Sandbox Code Playgroud)

//打印操作会显示操作字典,包括响应字段,//但是当我直接调用operation.response时,编译器将不会构建,说明//"找不到AFHTTPRequestOperation的属性".... WEIRDEST好的,对吧?

    NSString* responseString = [NSString stringWithUTF8String:[responseObject bytes]];
    //.. Rest o f my Code....

}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)

amb*_*amb 15

Hernan,如果您希望使用NSDictionaryJSON响应,则应考虑使用AFJSONRequestOperation,因为您在成功回调中获得了JSON字典.无论如何,如果你想从你的字典中获取字典,请responseObject尝试使用以下代码:

NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"Error serializing %@", error);
}
NSLog(@"Dictionary %@", JSON);
Run Code Online (Sandbox Code Playgroud)


Zha*_*ang 5

我相信响应字符串在"operation"对象中,所以类似于:

...
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", operation.responseString);
}];
Run Code Online (Sandbox Code Playgroud)