Sam*_*net 79 objective-c ios afnetworking afnetworking-2
我是obj-c的新手,并且一直在为我的一些项目使用asihttp.当在asihttp中做一个post请求时就这样做了.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:height forKey:@"user[height]"];
[request setPostValue:weight forKey:@"user[weight]"];
[request setDelegate:self];
[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)
怎么会这样做AFNetworking的代码示例?
我已经得到了让Json getrequest在AFNetworking工作,但这个帖子请求给了我一些问题.提前感谢您的帮助.
Jos*_*phH 209
它首先值得添加(因为这个答案在我最初编写它之后仍然流行了6年......)你要考虑的第一件事是你是否应该使用AFNetworking.在iOS 7中添加了NSURLSession,这意味着在许多情况下您不需要使用AFNetworking - 少一个第三方库总是一件好事.
对于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);
}];
Run Code Online (Sandbox Code Playgroud)
对于AFNetworking 2.0(以及使用新的NSDictionary语法):
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);
}];
Run Code Online (Sandbox Code Playgroud)
如果你使用AFNetworking 1.0卡住了,你需要这样做:
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);
}];
Run Code Online (Sandbox Code Playgroud)
Ale*_*x L 32
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
height, @"user[height]",
weight, @"user[weight]",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:@"http://localhost:8080/"]];
[client postPath:@"/mypage.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", text);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];
Run Code Online (Sandbox Code Playgroud)
Dan*_*Dan 12
这是我正在使用的一个简单的AFNetworking POST.为了在阅读AFNetworking doc,wkiki,ref等后启动并运行,我通过关注http://nsscreencast.com/episodes/6-afnetworking并了解github上的相关代码示例了解了很多.
// Add this to the class you're working with - (id)init {}
_netInst = [MyApiClient sharedAFNetworkInstance];
// build the dictionary that AFNetworkng converts to a json object on the next line
// params = {"user":{"email":emailAddress,"password":password}};
NSDictionary *parameters =[NSDictionary dictionaryWithObjectsAndKeys:
userName, @"email", password, @"password", nil];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:
parameters, @"user", nil];
[_netInst postPath: @"users/login.json" parameters:params
success:^(AFHTTPRequestOperation *operation, id jsonResponse) {
NSLog (@"SUCCESS");
// jsonResponse = {"user":{"accessId":1234,"securityKey":"abc123"}};
_accessId = [jsonResponse valueForKeyPath:@"user.accessid"];
_securityKey = [jsonResponse valueForKeyPath:@"user.securitykey"];
return SUCCESS;
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"FAILED");
// handle failure
return error;
}
];
Run Code Online (Sandbox Code Playgroud)