【发布时间】:2012-07-02 22:22:10
【问题描述】:
编辑 07/14
正如 Bill Burgess 在他的回答的评论中提到的那样,这个问题与 AFNetworking 的 version 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