我想使用异步请求获取json数据,我正在通过syncrouns来完成此操作,但现在需要更改,但我无法将此代码修改为异步,因为我必须返回NSdataJSON异步请求
+ (NSString *)stringWithUrl:(NSURL *)url
{
// if(kShowLog)
NSLog(@"%@", url);
NSURL *newURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:newURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:1];
// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// NSOperationQueue *opQueue;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}
你为什么不尝试使用[AFNetworking](http://afnetworking.com)? – cojoj
检查我的回答 – codercat
你有两个相互矛盾的要求:你必须返回的NSString对象,它是操作的结果,你的方法应是异步的。这是不可能的。调用方无法获取异步方法_immediately_的结果。修复你的需求。 – CouchDeveloper