AFNetworking: Can't get the response string from AFHTTPRequestOperation
Asked Answered
L

3

5

Anyone?): I'm having a problem that has made me scratch my head for the last 2 hours, and it most likely a very simple stupid thing I'm missing. I Keep getting a building error when I Call the response string from the operation @ AFNetworking... Like there is NO SUCH PROPERTY....

Please Take a look at my code and Explain me what did I Mess up This time :p.. THanks :)


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);

// Printing operation will show me the operation Dictionary, including the reponse field, // but when I Directly call operation.response, the Compiler won't Build, stating that // "Property not found for AFHTTPRequestOperation".... WEIRDEST THING EVER, right?

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

}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", error);
}];
Largo answered 11/12, 2012 at 0:43 Comment(5)
I'd Like to Simply print the operation.response in the NSLOG instead of the operation itself...Largo
Are you saying, that when you do NSLog(@"Response object %@",operation.response); - the compiler complains, but when you do NSLog(@"Operation object %@",operation); it prints and it has response as one of its elements. What is your console output.Shull
Have you tried printing out individual pieces of the operation response? For instance printing out the status code NSLog(@"Operation response status = %@", [NSHTTPURLResponse localizedStringForStatusCode:operation.response.statusCode]);Serow
@Srinkanth - The Console output is lke a Series of Hexadecimal Numbers.. not Really a Dictionary as I Expected... ThanksLargo
@Adam Johnson: That's the Thing... I Can't Access operation.response, nor operation.response.statusCode... Weird right? Thanks for the Reply :-)Largo
C
15

Hernan, if you expect an NSDictionary from a JSON response you should consider using AFJSONRequestOperation, because you get a JSON dictionary in your success callback. Anyway, if you want to get a dictionary from your responseObject, try to use the following code:

NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"Error serializing %@", error);
}
NSLog(@"Dictionary %@", JSON);
Cornea answered 13/12, 2012 at 8:16 Comment(3)
hi Amb: Thanks for responding Mate... I'll surely be trying the NSJSONSerialization for the Response Object, do I Place it as Is? Or Should I Convert it to UTF8 String? [NSString stringWithUTF8String:[responseObject bytes]]....Largo
Well, I Tried it, but the Server is Returning me a Response that Does Not Get Parsed OK by the JSON Parser.. I Get: 2012-12-16 19:17:14.536 Vidac[3561:19d03] Error serializing Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x94917d0 {NSDebugDescription=Invalid value around character 0.} Printing description of responseObject: <59397142 7871677a ...... > 2012-12-16 19:17:42.592 Vidac[3561:19d03] Dictionary (null)Largo
It seems that your server side is not returning a valid JSON object propertly, or that you have some problems with encoding. Try to copy and paste your JSON string response and check with a validator if everything is OK. Also, check NSJSONSerialization options.Cornea
E
5

I believe the response string is inside the "operation" object, so something like:

...
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", operation.responseString);
}];
Exequatur answered 3/4, 2014 at 7:35 Comment(0)
B
4

While attempting to retrieve content from meetup.com api using AFNetworking (kudos to Mattt T. for a great framework, btw), ran into the same error - "The operation couldn't be completed. (Cocoa error 3840)". Realized that the issue I was having was with the response data containing a Swedish character 'Ø', resulting in the parsing error. The solution was to include the header 'Accept-Charset: utf-8' in the initialization of the AFNetworking client. Fixed!

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    [self setDefaultHeader:@"Accept-Charset" value:@"utf-8"];

    return self;
}
Burnout answered 19/2, 2013 at 19:23 Comment(1)
"Ø" is danish. The Swedish version would be "Ö". :)Bruell

© 2022 - 2024 — McMap. All rights reserved.