【发布时间】:2015-09-17 15:49:58
【问题描述】:
我升级到IOS9 xcode,我的webservice获取答案的方法没有工作,这部分 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returnedResponse:&response error:&error];已弃用。
NSString *post =[[NSString alloc] initWithFormat:@"lang=%@&type=ssss",@"en"];
NSURL *url=[NSURL URLWithString:@"http://www.xxxxxxxxxx.com/xxxxxxx/ws/get_xxxxx.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"esta es la url: %@", postData);
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
if(success == 1)
{
if (self.lugares != nil) {
self.lugares = nil;
}
self.lugares = [[NSMutableArray alloc] initWithArray:[jsonData objectForKey:@"lugares"]];
}
else
{
NSLog(@"no hay datos :C");
}
}
else
{
NSLog(@"No encontrado");
}
【问题讨论】:
-
切换到NSURLSession。
-
为什么你已经放弃了对 iOS 8 的支持?
-
这个链接stackoverflow.com/questions/30739473/…解决了我的问题,谢谢大家!
标签: ios objective-c web-services