开发者

Multiple Requests on ASIHTTPRequest

I need to download three different sets of data from three different URLs. I decided to use ASIHTTPRequest. Two of the URLs are JSON feeds which I need to parse and one of them is a .txt file online that I need to store locally.

Now the example that is on ASIHTTPRequest's website for an asynchronous request shows the following:

- (IBAction)grabURLInBackground:(id)sender {
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

To pass multiple URLs, I can call "request" on three different URLs. But I am not sure how I would handle them in the requestFinishe开发者_JAVA技巧d method. The documentation shows it as:

- (void)requestFinished:(ASIHTTPRequest *)request {
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

How would this method differentiate between different requests, so that I can handle it differently?

Thank you,


You can differentiate between different requests by

  • setting the userInfo dictionary of the request
  • setting the didFinishSelector (and didFailSelector etc.) to different methods
  • using different classes as delegate
  • using blocks
  • using the request's tag property
  • subclass ASIHTTPRequest and override override requestFinished: and failWithError: (only recommended for complex situations)


You just need two snippets of code. One to 'tag' your url:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"identifierX", @"typeOfRequest",nil]];

and another one to identify it:

if ([[[request userInfo] valueForKey:@"typeOfRequest"] isEqualToString:@"identifierX"]){
        // Do here whatever you need to do for the url associated with identifierX
    }

and that should be it!


you can set the Username and tag of req.

this is the example of imageview. req.

UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];

    ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:[self.arr objectAtIndex:i]]];
    [req setUsername:[NSString stringWithFormat:@"%i",i]];
    [req setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:imgV,@"imgV",nil]];
    [req setDelegate:self];
    [req startAsynchronous];
    [imgV setContentMode:UIViewContentModeScaleToFill];
    [imgV setClipsToBounds:YES];
    [imgV setTag:kTagImageViewInScrollView];
    [scr2 addSubview:imgV];
    [scr2 setDelegate:self];
    [imgV release]; imgV=nil;

and in requestFinished

- (void)requestFinished:(ASIHTTPRequest *)request {
    [(UIImageView*)[[request userInfo] valueForKey:@"imgV"] setImage:[UIImage imageWithData:[request responseData]]];   

}


You can check the originalURL property of ASIHTTPRequest if you have different URLS.

Or you can use [request hash] to get the NSObject hash for each object and check that later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜