如何在iphone中使用多个代理?

如何在iphone中使用多个代理?,iphone,json,Iphone,Json,我使用下面的代码来使用json,但我需要在同一个页面中有更多的url连接,如何实现它,提前谢谢 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d

我使用下面的代码来使用json,但我需要在同一个页面中有更多的url连接,如何实现它,提前谢谢

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}

- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];

}

-(void)searchForStuff:(NSString *)text
 {
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL   URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

我正在使用php进行web访问

您可以使用实例变量来保留指向连接的指针。然后在委托回调中,检查指针是否相等,以检查正在处理的连接。

您可以使用实例变量保留指向连接的指针。然后在委托回调中,检查指针是否相等,以检查正在处理的连接。

不要这样做

取而代之的是使用brilliant库,它使一切变得更简单、更好。实际上,自从我几年前发现ASI以来,我没有写过一个NSURLConnection,一个也没有

ASI的块接口允许您在启动请求对象之前使用其处理程序代码配置请求对象,并且不需要任何委托

__block ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myNSURLObject];
[r setCompletionBlock:^{
    NSLog([r responseString]); //for instance
}];
[r startAsynchronous];
如果块让您感到害怕,您还可以将特定请求指向特定方法,这样就可以分别处理不同的请求类型:

- (void) viewDidLoad { //or wherever
    ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myFirstURL];
    r.delegate = self;
    [r setDidFinishSelector:@selector(requestDone:)];
    [r startAsynchronous];
}

// then later on...
- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}
不要这样做

取而代之的是使用brilliant库,它使一切变得更简单、更好。实际上,自从我几年前发现ASI以来,我没有写过一个NSURLConnection,一个也没有

ASI的块接口允许您在启动请求对象之前使用其处理程序代码配置请求对象,并且不需要任何委托

__block ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myNSURLObject];
[r setCompletionBlock:^{
    NSLog([r responseString]); //for instance
}];
[r startAsynchronous];
如果块让您感到害怕,您还可以将特定请求指向特定方法,这样就可以分别处理不同的请求类型:

- (void) viewDidLoad { //or wherever
    ASIHTTPRequest *r = [ASIHTTPRequest requestWithUrl:myFirstURL];
    r.delegate = self;
    [r setDidFinishSelector:@selector(requestDone:)];
    [r startAsynchronous];
}

// then later on...
- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}

由于NSValue符合
NSCopying
的要求,我使用它来包装指向连接的指针,并将其用作从
NSMutableDictionary
访问相关数据的键。例如,您可以执行以下操作:

-(void)searchForStuff:(NSString *)text withTarget:(id)target selector:(SEL)selector {
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:target,@"target",NSStringFromSelector(selector),@"selector",nil];
    NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [myMutableDictionary setObject:options forKey:[NSValue valueWithPointer:c]];
    [c release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSValue *p = [NSValue valueWithPointer:connection];
    NSDictionary *options = [myMutableDictionary objectForKey:p];
    if (options) {
        id target = [options objectForKey:@"target"];
        SEL selector = NSSelectorFromString([options objectForKey:@"selector"]);
        if (target && selector && [target respondsToSelector:selector]) {
            [target performSelector:selector withObject:responseData];
        }
    }
}

由于NSValue符合
NSCopying
的要求,我使用它来包装指向连接的指针,并将其用作从
NSMutableDictionary
访问相关数据的键。例如,您可以执行以下操作:

-(void)searchForStuff:(NSString *)text withTarget:(id)target selector:(SEL)selector {
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:target,@"target",NSStringFromSelector(selector),@"selector",nil];
    NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [myMutableDictionary setObject:options forKey:[NSValue valueWithPointer:c]];
    [c release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSValue *p = [NSValue valueWithPointer:connection];
    NSDictionary *options = [myMutableDictionary objectForKey:p];
    if (options) {
        id target = [options objectForKey:@"target"];
        SEL selector = NSSelectorFromString([options objectForKey:@"selector"]);
        if (target && selector && [target respondsToSelector:selector]) {
            [target performSelector:selector withObject:responseData];
        }
    }
}

实际上,您不需要多个代理。您需要多个NSURLConnection,您可以测试哪一个正在调用委托方法

比如说。假设以下实例变量(或属性):

首先,实例化每个NSURLConnection变量

-(void)searchA:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.a.com/%@", text]]];
    connectionA = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void)searchB:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.b.com/%@", text]]];
    connectionB = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
然后,您可以测试哪个连接正在调用委托方法,并基于该连接自定义实现

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection == connectionA) {
        [dataA appendData:data];
    }
    else if (connection == connectionB) {
        [dataB appendData:data];
    }
}

您需要为每个委托方法执行此操作。

您实际上不需要多个委托。您需要多个NSURLConnection,您可以测试哪一个正在调用委托方法

比如说。假设以下实例变量(或属性):

首先,实例化每个NSURLConnection变量

-(void)searchA:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.a.com/%@", text]]];
    connectionA = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void)searchB:(NSString *)text
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.b.com/%@", text]]];
    connectionB = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
然后,您可以测试哪个连接正在调用委托方法,并基于该连接自定义实现

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection == connectionA) {
        [dataA appendData:data];
    }
    else if (connection == connectionB) {
        [dataB appendData:data];
    }
}

每个委托方法都需要这样做。

虽然我喜欢基于块的方法(并希望NSURLConnection使用它),但我会避免使用ASIHTTPRequest,因为开发人员不再支持它。还有一些类似的库仍然受到支持。有关更多详细信息,请参见ASIHTTPRequest的博客@codecaffine-Wow。。。。我不知道。天啊,我觉得好像一个家庭成员死了什么的。呼。我想我得开始四处寻找替代者了。我不可能回到原始的痛苦。是的,我认为基于块的url加载更有意义,也更容易。我希望苹果能添加类似于NSURLConnection的东西。博客中提到了几个备选方案。虽然我喜欢基于块的方法(并希望NSURLConnection使用它),但我会避免使用ASIHTTPRequest,因为开发人员不再支持它。还有一些类似的库仍然受到支持。有关更多详细信息,请参见ASIHTTPRequest的博客@codecaffine-Wow。。。。我不知道。天啊,我觉得好像一个家庭成员死了什么的。呼。我想我得开始四处寻找替代者了。我不可能回到原始的痛苦。是的,我认为基于块的url加载更有意义,也更容易。我希望苹果能添加类似于NSURLConnection的东西。博客中提到了几个备选方案。好吧,我建议你先做一些简单的事情,掌握Objective C的诀窍,或者学习一些教程,然后自己尝试一下;)。或者,使用Dan Rays优秀的解决方案。好吧,我建议您先构建一些简单的东西,掌握Objective C的窍门,也许遵循一些教程,然后自己尝试一下;)。或者,只需使用Dan Rays优秀的解决方案。您可以用与connection:didReceiveData:类似的方式编写所有委托方法。使用“if…else if”可根据每个连接自定义行为。您可以以类似的方式将所有委托方法写入连接:didReceiveData:。使用“if…else if”根据每个连接自定义行为。