首先,所有的问题都是失败。如果你只想看他们是什么跳到这篇文章的底部,你会看到他们用粗体..。关于更多细节,你可以阅读这篇文章的其余部分.
我只是试着把我的NSURLConnection熨平,以便它能顺利地工作,我对此有正确的理解。对于互联网上的异步连接,我们非常缺乏例子/教程,也没有任何我能找到的解释,除了建立和运行连接之外,任何深度都在发生着什么,在做完之后,这看起来相当简单。希望这个问题能填补我觉得是其他用户的空白。
因此,在我的.h文件中,我导入了基金会头,并声明了接收或缺少接收数据(错误等)所需的方法。
.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> //add foundations
//.. other headers can be imported here
@interface MyViewController: UITableViewController {
//Im not setting any delegates to access the methods because Is all happening in the same
//place so I just use the key word 'self' when accessing the methods declared below
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help me with the way I have set up my request etc.
}
- (IBAction)setRequestString:(NSString *)string; //this method sets the request and connection methods
//these methods receive the response from my async nsurlconnection
- (void)receivedData:(NSData *)data;
- (void)emptyReply;
- (void)timedOut;
- (void)downloadError:(NSError *)error;那是我的头文件..。很简单,不需要太多的解释。
.m
//call setRequestString from some other method attached to a button click or something
[self setRequestString:@"rss.xml"];
//..
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http:www.comicbookresources/feeds/"]; // address not real jsut example
//append the string coming in to the end of the databaseURL
[databaseURL appendString:string];
//prepare NSURL with newly created string
NSURL *url = [NSURL URLWithString:databaseURL];
//AsynchronousRequest to grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil){
[self receivedData:data];
}else if ([data length] == 0 && error == nil){
[self emptyReply];
}else if (error != nil && error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses
[self timedOut];
}else if (error != nil){
[self downloadError:error];
}
}];
}现在,设置在.h文件中初始化并在上述if语句中调用的方法。
- (void)receivedData:(NSData *)data
{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr); //logs recived data
//now you can see what data is coming in on your log
//do what you want with your data here (i.e. start parsing methods
}
- (void)emptyReply
{
//not sure what to do here yet?
}
- (void)timedOut
{
//also not sure what to do here yet?
}
- (void)downloadError:(NSError *)error
{
NSLog(@"%@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A connection failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}很酷,这就是我在那里所做的事情的基本内容。现在我的问题如下。
问题一:我这样称呼NSURLConnection的
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{这里发生了什么?为什么^在不同的线程上执行整个块(包括if语句)?因为它看起来很像大中央调度格式,但略有不同。
问题二:在emptyReply & timedOut方法中应该做什么?
问题三:如何将缓存集成到其中?我想缓存从不同请求中得到的回复。也就是说,使用我的setRequestString,您将看到有一个字符串输入参数,因此我可以使用相同的方法请求不同的rss提要。我需要弄清楚如何将这些响应缓存到单独的缓存中。但我不知道从哪里开始。
,如果你已经做到了这一点,非常感谢你阅读了我的问题。希望通过你的回应,我们能找到一个很好的解决方案。其他人可以自己使用,挑选和选择他们需要的适合自己的解决方案的部分和部分。
无论如何,非常感谢你的阅读,我期待着你的答复。即使它们只是对你认为可能对我有帮助的教程或例子的参考。任何事情都是好的,我只想充分了解发生了什么,什么是好的解决方案。
发布于 2012-02-15 02:05:36
https://stackoverflow.com/questions/9286849
复制相似问题