AFNetworking – 如何发出POST请求

编辑07/14

正如Bill Burgess在回答他的评论时提到的,这个问题与AFNetworking version 1.3有关。 这里的新手可能已经过时了。


我对iPhone开发非常陌生,我正在使用AFNetworking作为我的服务库。

我查询的API是一个RESTful的,我需要做POST请求。 要做到这一点,我试着用下面的代码:

 NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Pass Response = %@", JSON); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Failed Response : %@", JSON); }]; [operation start]; 

这个代码有两个主要的问题:

  • AFJSONRequestOperation似乎做了一个GET请求,而不是一个POST
  • 我不能把这个方法的参数。

我也试过这个代码:

 NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; [httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Succes : %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure : %@", error); }]; 

有没有更好的方法来使我想要完成的任务?

谢谢您的帮助 !

您可以覆盖与AFNetworking一起使用的请求的默认行为,以作为POST进行处理。

 NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil]; 

这假定您已经覆盖默认的AFNetworking设置来使用自定义客户端。 如果你不是,我会build议这样做。 只需创build一个自定义类来处理您的networking客户端。

MyAPIClient.h

 #import <Foundation/Foundation.h> #import "AFHTTPClient.h" @interface MyAPIClient : AFHTTPClient +(MyAPIClient *)sharedClient; @end 

MyAPIClient.m

 @implementation MyAPIClient +(MyAPIClient *)sharedClient { static MyAPIClient *_sharedClient = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:webAddress]]; }); return _sharedClient; } -(id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (!self) { return nil; } [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; [self setDefaultHeader:@"Accept" value:@"application/json"]; self.parameterEncoding = AFJSONParameterEncoding; return self; } 

那么你应该可以在运行队列上触发你的networking调用,没有任何问题。

  MyAPIClient *client = [MyAPIClient sharedClient]; [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount]; NSString *path = [NSString stringWithFormat:@"myapipath/?value=%@", value]; NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { // code for successful return goes here [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount]; // do something with return data }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { // code for failed request goes here [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount]; // do something on failure }]; [operation start];