网络队列未响应第二个请求

时间:2012-07-04 05:17:52

标签: iphone objective-c json asihttprequest

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

-(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 *)请求方法, 它总是将第一个xml数据显示为response(NSLog(@“%@”,responseString);), 如果我将我的密钥从requestnum更改为requestnum1,我将第一个xml数据作为responceString获取,为什么我无法访问第二个请求的响应。

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

1 个答案:

答案 0 :(得分: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;
    }
}

享受。