我有一个php页面,它返回以下信息: -
[{“ID”:“1234ABCD”,“name”:“Terry Tate”,“gender”:“M”}]
我想把它拉到xCode并解析它。我看到的所有教程都非常复杂,似乎是为了从数据库中提取多条记录。这不是我所关心的复杂性,我只是不想写不必要的代码。所以我想知道是否有一种更简单,更快捷的方法来实现这一点,而无需设置所有连接协议,如didRecieveData,connectionDidFinishLoading等,以及不沿着那条路线下行的风险是什么?
目前我正在使用此代码检索数据: -
NSURL *url = [NSURL URLWithString:@"http://www.testServer.com/getData.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *body = [@"uID=1234ABCD" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; // not necessary, but good practice
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// use the data/error/response objects here
if (data) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responseString = %@", responseString);
} else {
NSLog(@"error = %@", error);
NSLog(@"response = %@", response);
}
}];
[task resume];