AFJSONRequestOperation在iOS中返回空响应
在使用AFJSONRequestOperation时,我面临1个问题。我的API客户端如下AFJSONRequestOperation在iOS中返回空响应,json,web-services,ios6,afnetworking,Json,Web Services,Ios6,Afnetworking,在使用AFJSONRequestOperation时,我面临1个问题。我的API客户端如下 #import <Foundation/Foundation.h> #import "AFHTTPClient.h" #import "AFJSONRequestOperation.h" @interface MyAPIClient : AFHTTPClient + (MyAPIClient *)sharedAPIClient; @end // .m file #import
#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
但是,当我使用AFHTTPRequestOperation时,它会将正确的输出与登录的已用信息一起返回
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];
我的服务器返回JSON响应。
上面代码中的问题是什么?为什么AFJSONRequestOperation在
AFHTTPRequestOperation返回正确的响应?感谢您的任何帮助。提前感谢我最终使用了AFHTTPRequestOperation而不是AFJSONRequestOperation。
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];