-(IBAction) webRequest;
{
response = [[NSMutableData alloc] init];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:kResourcesURL]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"accept"];
[theRequest setValue:strToken forHTTPHeaderField:@"token"];
//show network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlresponse error:&nserror];
NSString *strResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJsonParser *sbjasonparser = [[SBJsonParser alloc] init];
arrResponse = [sbjasonparser objectWithString:strResponse error:nil];
//hide network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
我的代码有问题吗?或者同步请求有什么区别吗? 有什么建议吗?或者我应该使用ActivityIndicator视图?
NSURLConnectionDelegate
方法。如果你选择了后台线程或NSOperationQueue
路线,请使用-performSelectorOnMainThread:withObject:waitUntilDone:
来调用你的视图配置代码。如果你能承受 iOS 5 以上版本的代码,将视图配置放入+sendAsynchronousRequest:queue:completionHandler:
的完成块中。 - CostiqueNSOperationQueue
在iOS 2.0及更高版本中可用。但是,某些方法是在iOS 4.0中添加的。 - Costique