我会为所有请求创建一个 APIClient 类,而不是每次发出请求时都创建一个新客户端。
请参阅:https ://github.com/AFNetworking/AFNetworking/tree/master/Example/Classes
AFTwitterAPIClient.h & AFTwitterAPIClient.m
但根据你的问题。我相信代码看起来像这样。(代码未经测试)
NSURL *url = [NSURL URLWithString:@"http://server.com"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
//depending on what kind of response you expect.. change it if you expect XML
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
@"NUMBER",@"number",
@"NAME",@"name",
@"32.5713",@"lat",
@"60.3926",@"lon",
nil];
[client putPath:@"users" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure");
}];
至于发布请求..只需使用 postPath 而不是 putPath 就可以了。:)
希望我有所帮助。