以下是我用于在显示UIActivityIndicator时从服务器检索数据的方法。我试图把这些方法放在应用程序委托中,然后从其他类中调用它们,但我不知道如何返回我的JSONData。任何人都可以帮忙吗?objective-c:从服务器返回数据
-(void)startProcess:(NSString *)buildURL{
UIActivityIndicatorView *aInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActionSheetStyleBlackTranslucent];
[aInd setFrame:CGRectMake(0, 0, 50, 50)];
[aInd startAnimating];
// then call the timeCOnsumingmethod in separate thread.
[NSThread detachNewThreadSelector:@selector(getData:) toTarget:self withObject:buildURL];
}
- (void)getData:(NSString *)buildURL{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Query our database for a restaurant's menus
NSURL *url = [NSURL URLWithString:buildURL];
NSError *e;
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&e];
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
// NSError *error = nil;
[self performSelectorOnMainThread:@selector(endProcess:) withObject:jsonData waitUntilDone:YES];
[pool release];
//return jsonData;
}
- (IBAction)endProcess:(NSData *)jsonData{
// ??????????
return jsonData;
}
嗯......为什么不为你的web服务请求使用异步API,而不是复杂的'detachNewThreadSelector:toTarget:withObject:'它比来回地释放'performSelector ...'更有意义。我将通过一些异步使用NSURLRequest或第三方网络库的例子。 [这是一个](http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/) – FluffulousChimp 2012-03-20 19:05:08
这个教程很好,但我将如何改变它,以便我在一个单独的类中实现这些方法? – Apollo 2012-03-20 19:15:24