AFJSONRequestOperation returns null response in iOS
I ran into one problem while using AFJSONRequestOperation. My API client looks like this
#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"
@interface MyAPIClient : AFHTTPClient
+ (MyAPIClient *)sharedAPIClient;
@end
// .m file
#import "MyAPIClient.h"
@implementation MyAPIClient
+ (MyAPIClient *)sharedAPIClient {
static MyAPIClient *sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[MyAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kBASE_URL]];
});
return sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:CONTENT_TYPE_FIELD value:CONTENT_TYPE_JSON_VALUE];
self.parameterEncoding = AFJSONParameterEncoding;
return self;
}
@end
Now when I ask for the following code, it returns me a "null" response
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:userName,@"email",password,@"password",nil];
NSMutableURLRequest *request = [[MyAPIClient sharedAPIClient] requestWithMethod:@"POST" path:@"login" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSDictionary *dictResponse = (NSDictionary*)JSON;
DLog(@"Login Success JSON: %@",JSON);
if (block) {
block(YES,nil);
}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
DLog(@"Login Error:- %@",error);
if (block) {
block(NO,error);
}
}];
[operation start];
But when I use AFHTTPRequestOperation it gives me correct output with logged in information used
NSURL *loginUrl = [NSURL URLWithString:kBASE_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:loginUrl];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
userName,@"email",password,@"password",nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"login" parameters:params];
//Notice the different method here!
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Raw data Response: %@", responseObject);
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSLog(@"Converted JSON : %@", jsonArray);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}];
//Enqueue it instead of just starting it.
[httpClient enqueueHTTPRequestOperation:operation];
My server is returning a JSON response. What is the problem in the above code? Why does AFJSONRequestOperation return a null response while AFHTTPRequestOperation returns the correct response to me? Any help is appreciated. thanks in advance
+1
source to share
No one has answered this question yet
See similar questions:
or similar: