编辑 07/14
正如比尔·伯吉斯(Bill Burgess)在他的回答的评论中提到的,这个问题与version 1.3
of AFNetworking
。对于这里的新人来说可能已经过时了。
我对 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
one.
- 我无法向此方法添加参数。
我也尝试过使用这段代码:
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);
}];
有没有更好的方法来完成我想要的事情?
谢谢您的帮助 !