I've setup a JSON post with AFNetworking
in Objective-C and am sending data to a server with the following code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"name": deviceName, @"model": modelName, @"pin": pin};
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"Content-Type" forHTTPHeaderField:@"application/json"];
[manager POST:@"SENSORED_OUT_URL" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
I'm receiving information through the same request, and want to send the data to a NSString
. How would I go about doing that with AFNetworking
?
responseObject
is either an NSArray or NSDictionary. You can check at runtime using isKindOfClass:
:
if ([responseObject isKindOfClass:[NSArray class]]) {
NSArray *responseArray = responseObject;
/* do something with responseArray */
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDict = responseObject;
/* do something with responseDict */
}
If you really need the string of the JSON, it's available by looking at operation.responseString
.
In this case, when the web service responds with JSON
, the AFNetworking
will do the serialization for you and the responseObject
will most likely be either a NSArray
or NSDictionary
object.
Such an object should be more useful for you than string with JSON
content.
In my case, it's looks like (maybe it can helps)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
//!!! here is answer (parsed from mapped JSON: {"result":"STRING"}) ->
NSString *res = [NSString stringWithFormat:@"%@", [jsonDict objectForKey:@"result"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//....
}
];
Also would be great to check type of response object (like https://stackoverflow.com/a/21962445/3628317 answer)
I find it works best to subclass AFHTTPClient like so:
// MyHTTPClient.h
#import <AFNetworking/AFHTTPClient.h>
@interface MyHTTPClient : AFHTTPClient
+ (instancetype)sharedClient;
@end
// MyHTTPClient.m
#import "MyHTTPClient.h"
#import <AFNetworking/AFJSONRequestOperation.h>
static NSString *kBaseUrl = @"http://api.blah.com/yada/v1/";
@implementation MyHTTPClient
+ (instancetype)sharedClient {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
if (self = [super initWithBaseURL:[NSURL URLWithString:kBaseUrl]]) {
self.parameterEncoding = AFJSONParameterEncoding;
[self setDefaultHeader:@"Accept" value:@"application/json"]; // So AFJSONRequestOperation becomes eligible for requests.
[self registerHTTPOperationClass:[AFJSONRequestOperation class]]; // So that it gets used for postPath etc.
}
return self;
}
@end
The important bits are:
Then you can use it like so:
#import "MyHTTPClient.h"
@implementation UserService
+ (void)createUserWithEmail:(NSString *)email completion:(CreateUserCompletion)completion {
NSDictionary *params = @{@"email": email};
[[MyHTTPClient sharedClient] postPath:@"user" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
completion([responseObject[@"userId"] intValue], YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(0, NO);
}];
}
@end
The beauty of this is that your responseObject is automatically JSON-parsed into a dictionary (or array) for you. Very clean.
(this is for afnetworking 1.x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With