0

我正在创建一个网络队列来发送两个请求。我已将这两个请求添加到网络队列中。

-(void)viewDidLoad 
{
    [super viewDidLoad];
    ASINetworkQueue *networkQueue = [[ASINetworkQueue queue] retain];
    NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/xml/simple.xml"];
    NSURL *url1 = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp"];

    for(int i = 0; i<=1; i++)
    {
        if(i==0)
        {
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum"];
            [request setDelegate:self];
            [networkQueue addOperation:request];
        }
        if(i==1)
        {
            ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request1.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum1"];
            [request1 setDelegate:self];
            [networkQueue addOperation:request1];   
        }
        [networkQueue go];
    }
}

为了检查我的响应,我在 requestFinished:(ASIHTTPRequest *)request 方法中得到响应,如果我将密钥从 requestnum 更改为requestnum1 我将第一个 xml 数据作为响应字符串获取,为什么我无法访问第二个请求的响应。

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *str ;
    str= [request.userInfo objectForKey:@"requestnum1"];
    NSString *responseString = [request responseString];
    str = [request responseString];
    NSLog(@"%@",responseString);
}
4

1 回答 1

1

区分这两个请求非常简单。只需为每个请求分配标签。

for(int i = 0; i<=1; i++)
{
    if(i==0)
    {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        NSString *str =[NSString stringWithFormat: @"%d", i];
        request.tag = 1; //<--Tag here
        request.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum"];
        [request setDelegate:self];
        [networkQueue addOperation:request];
    }
    if(i==1)
    {
        ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
        NSString *str =[NSString stringWithFormat: @"%d", i];
        request1.tag = 2; //<--Tag here
        request1.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum1"];
        [request1 setDelegate:self];
        [networkQueue addOperation:request1];   
    }
    [networkQueue go];
}

在请求完成处理相应

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"request tag-->%d",request.tag);

    NSString *str=nil;
    NSString *responseString = nil;

    switch (request.tag) 
    {
        case 1:

            str= [request.userInfo objectForKey:@"requestnum"];
            responseString = [request responseString];
            str = [request responseString];
            NSLog(@"Response reqeust 1 --->%@",responseString);

            break;

        case 2:
            str= [request.userInfo objectForKey:@"requestnum1"];
            responseString = [request responseString];
            NSLog(@"Response reqeust 2 --->%@",responseString);

            break;
        default:
            break;
    }
}

享受。

于 2012-07-04T05:29:53.243 回答