AFNetworking 发布请求 [英] AFNetworking Post Request
问题描述
我是 obj-c 的新手,并且一直在我的一些项目中使用 asihttp.在 asihttp 中执行 post 请求时,它就是这样完成的.
I'm a newbie in obj-c and have been using asihttp for some of my projects. When doing a post request in asihttp its done this way.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:height forKey:@"user[height]"];
[request setPostValue:weight forKey:@"user[weight]"];
[request setDelegate:self];
[request startAsynchronous];
AFNetworking 将如何使用代码示例执行此操作?
我已经在 AFNetworking 中使用了 get Json getrequest,但是这个 post 请求给我带来了一些问题.提前感谢您的帮助.
How would go about doing this is AFNetworking with a code example ?
I already got the get Json getrequest working in AFNetworking but this post request is giving me some problems. Thanks for help in advance.
推荐答案
首先值得补充的是(因为这个答案在我最初写它的 6 年后仍然很受欢迎......)你应该考虑的第一件事是你是否应该甚至使用 AFNetworking.NSURLSession 是在 iOS 7 中添加的,这意味着在许多情况下您不需要使用 AFNetworking - 少一个第三方库总是一件好事.
It's first worth adding (as this answer is still popular 6 years after I initially wrote it...) that the first thing you should consider is whether you should even use AFNetworking. NSURLSession was added in iOS 7 and means you don't need to use AFNetworking in many cases - and one less third party library is always a good thing.
对于 AFNetworking 3.0:
For AFNetworking 3.0:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"user[height]": height,
@"user[weight]": weight};
[manager POST:@"https://example.com/myobject" parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
对于 AFNetworking 2.0(并且还使用新的 NSDictionary 语法):
For AFNetworking 2.0 (and also using the new NSDictionary syntax):
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"user[height]": height,
@"user[weight]": weight};
[manager POST:@"https://example.com/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
如果您在使用 AFNetworking 1.0 时遇到困难,您需要这样做:
If you are stuck using AFNetworking 1.0, you need to do it this way:
NSURL *url = [NSURL URLWithString:@"https://example.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
height, @"user[height]",
weight, @"user[weight]",
nil];
[httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];
这篇关于AFNetworking 发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!