iPhone:使用 NSURL 请求异步下载数据,而不是 [NSData datawithContents ofURL]
您好,我是一名新手,我正在尝试从 googleapi 网页下载地理编码数据。我使用以下代码做到了这一点:代码:
NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp];
NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address];
[address appendString:@" "];
[address appendString:l.CityName];
[address appendString:@" "];
[address appendString:l.State];
[address appendString:@" "];
[address appendString:l.PostalCode];
NSArray *addressArray = [address componentsSeparatedByString:@" "];
[address release];
NSString *a = [addressArray componentsJoinedByString:@"+"];
[urlStr appendString:a];
[urlStr appendString:@"&sensor=false"];
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL:url];
它工作得很好,但问题是它锁定了我的 GUI,因为它都是同步完成的。
有人建议我使用 NSURLrequest 异步执行此操作。于是我就去了苹果网站。从那里我看到了他们的例子。然而,我还是卡住了。如何将上面的同步请求代码转换为异步请求?到目前为止,这就是我所拥有的,但我不确定这是否有效......有点困惑......有人可以用这段代码为我指出正确的方向吗?
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//create NSURL request for asynchronous processing
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
receivedData = [[NSMutableData data] retain];
}
else
{
// Inform the user that the connection failed.
}
Hi Im a newbie and Im trying to download geocoding data from googleapi webpage. I did it using this: code:
NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp];
NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address];
[address appendString:@" "];
[address appendString:l.CityName];
[address appendString:@" "];
[address appendString:l.State];
[address appendString:@" "];
[address appendString:l.PostalCode];
NSArray *addressArray = [address componentsSeparatedByString:@" "];
[address release];
NSString *a = [addressArray componentsJoinedByString:@"+"];
[urlStr appendString:a];
[urlStr appendString:@"&sensor=false"];
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL:url];
It worked fine but the glitch was that it locked up my GUI since it was all done synchronously.
Someone suggested I do it asynchronously using NSURLrequest. So I went to the apple website. from there I saw their example. However, Im still stuck. How do I convert the above code which is a synchronous request to an asynchronous one? So far this is what I have, but Im not sure if this works...... a bit confused ... could someone please point me in the righ direction with this code?
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//create NSURL request for asynchronous processing
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
receivedData = [[NSMutableData data] retain];
}
else
{
// Inform the user that the connection failed.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NSURLConnection
根据您构建代码的方式,您需要在成功完成 (connectionDidFinishLoading:) 或失败 (connection: didFailWithError:) 时采取行动或通知其他对象(委托、通知)。
Using NSURLConnection
Depending on how you structure your code, you'll want to act or notify some other object (delegate, notification) on successful completion (connectionDidFinishLoading:) or failure (connection: didFailWithError:).