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 requestFinished
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
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]]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With