2012-07-04 3 views
0

네트워크 큐를 만들어 두 요청을 보내고 있습니다.이 두 요청을 네트워크 큐에 추가했습니다. 에 내가 responce을 얻고 내 responce 확인을위한네트워크 큐가 두 번째 요청에 응답하지 않습니다.

-(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 *) 요청 메도, 항상 responce로 첫 번째 XML 데이터 표시 (NSLog ("%의 @", responseString) @를) , requestnum에서 내 key를 requestnum1로 변경하면 responseString으로 첫 xml 데이터를 얻습니다. 두 번째 요청의 응답에 액세스 할 수없는 이유는 무엇입니까?

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

@all 도와주세요! –

+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; 
    } 
} 

즐기 처리합니다.

관련 문제