【问题标题】:AFNetworking - How to make POST requestAFNetworking - 如何发出 POST 请求
【发布时间】:2012-07-02 22:22:10
【问题描述】:

编辑 07/14

正如 Bill Burgess 在他的回答的评论中提到的那样,这个问题与 AFNetworkingversion 1.3 有关。对于这里的新人来说可能已经过时了。


我是 iPhone 开发的新手,我使用 AFNetworking 作为我的服务库。

我正在查询的 API 是一个 RESTful API,我需要发出 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);
}];

有没有更好的方法来完成我想要的工作?

感谢您的帮助!

【问题讨论】:

    标签: iphone objective-c ios json afnetworking


    【解决方案1】:

    您可以覆盖与 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];
    

    【讨论】:

    • 感谢您的回复!只有一个问题:你的SIOAPIClient 会是MyAPIClient 这里不是吗?
    • 你是对的......我从我自己的实现中复制/粘贴了这个并更改了类名。我猜错过了一个地方。我编辑了我的答案。
    • 我尝试在您编写时实现自定义客户端,但出现错误:No known class method for selector 'sharedClient'。标头包含正确,所以我想知道为什么会出现此错误。
    • 我添加了可能有助于解决此问题的标题信息。
    • 对于最新版本的 AFNetworking,此答案现在可能已过时。创建此答案时,它适用于 AFNetworking 1.3 左右。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多