AFNetworking - 如何发出POST请求

时间:2012-07-02 13:33:15

标签: iphone objective-c ios json afnetworking

编辑07/14

正如比尔·伯吉斯在评论中提到的那样,这个问题与version 1.3的{​​{1}}有关。这里的新人可能已经过时了。


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

我正在查询的API是一个RESTful,我需要发出POST请求。为此,我尝试使用以下代码:

AFNetworking

此代码存在两个主要问题:

  • 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

有没有更好的方法来制作我想要的东西来完成它?

感谢您的帮助!

1 个答案:

答案 0 :(得分:24)

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

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

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

<强> 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;

}

然后你应该能够在没有问题的情况下在操作队列上启动网络调用。

    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];