【发布时间】:2014-03-10 05:32:35
【问题描述】:
我对 IOS 开发真的很陌生..我只想从我的 Web 服务中获得一些 JSON 响应。当我搜索它时,我发现 AFnetworking 对此很有用。所以我下载了它并做了一个示例应用程序。但我需要将一些参数传递给服务。我的服务有两个参数 [用户名和密码]。我的网址是这样的
http://myweb.mobile.com/WebServices/AppointmentService.asmx/GetValidUser
谁能给我一些代码示例,如何将我的参数传递给该服务以获取 json 响应?
我已经尝试过这段代码.. 但它不会接受参数.. 一些人在谈论 AFHTTPClient.. 但我使用的是 AFN 2.0.. 这里没有类调用 AFHTTPCLient .. 我在这里很容易......请帮帮我
NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=restuarants+in+sydney&sensor=false&key=AIzaSyDn9a-EvJ875yMxZeINmUP7CwbO9YT1w2s"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 3
self.googlePlaces = [responseObject objectForKey:@"results"];
[self.tableView reloadData];
// NSLog(@"JSON RESULT %@", responseObject);
self.weather = (NSDictionary *)responseObject;
self.title = @"JSON Retrieved";
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
编辑:
我已经使用下面的代码段解决了我的问题...
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"47", @"caregiverPersonId", nil];
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[operationManager POST:@"your full url goes here"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
}
];
【问题讨论】:
-
在 2.0 中,您可以使用
AFHTTPRequestOperationManager而不是AFHTTPClient。然后您可以使用POST或GET(或任何适合您的网络服务的东西)。 -
非常感谢您的指导,此链接解决了我的问题*.com/questions/20047090/…
标签: ios afnetworking-2