AFJSONRequestOperation returns null response in iOS

谁都会走 提交于 2020-01-05 04:30:07

问题


I am facing 1 problem while using AFJSONRequestOperation. My API Client is as follows

    #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 request with following code it returns me "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 reurns me correct output with logged in used info

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 returns JSON response. What is the problem in above code? Why AFJSONRequestOperation returns null response while AFHTTPRequestOperation returns me correct response ? Any kind of help is appreciated. Thanks in advance

来源:https://stackoverflow.com/questions/14393131/afjsonrequestoperation-returns-null-response-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!