pmerino
pmerino

Reputation: 6130

How to do different actions depending on ASIHTTPRequest?

I'm using ASIFormDataRequest for uploading an image to TwitPic and I get an response, from here all ok. But on - (void)requestFinished:(ASIHTTPRequest *)request I already have an action for a TwitLonger response (which works correctly). Now how would I do a different action depending of the type of response? I tried setting an string and comparing with if, getting the last thing of the response, but with no luck. This is the way I tried:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];

    NSString *first = [responseString substringFromIndex: [responseString length] - 7];
    NSLog(@"%@", first);
    if (first == @"</rsp>"+
        ) {
        NSString *responseString = [request responseString];
        NSLog(@"%@", responseString);
        NSString *result = nil;
        // Determine "<div>" location
        NSRange divRange = [responseString rangeOfString:@"<mediaurl>" options:NSCaseInsensitiveSearch];
        if (divRange.location != NSNotFound)
        {
            // Determine "</div>" location according to "<div>" location
            NSRange endDivRange;

            endDivRange.location = divRange.length + divRange.location;
            endDivRange.length   = [responseString length] - endDivRange.location;
            endDivRange = [responseString rangeOfString:@"</mediaurl>" options:NSCaseInsensitiveSearch range:endDivRange];

            if (endDivRange.location != NSNotFound)
            {
                // Tags found: retrieve string between them
                divRange.location += divRange.length;
                divRange.length = endDivRange.location - divRange.location;

                result = [responseString substringWithRange:divRange];
            }
            tweet.text = result;
            NSLog(@"%@", result);
        }
    } else {

    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@"%@", responseString);
    NSString *result = nil;
        // Determine "<div>" location
        NSRange divRange = [responseString rangeOfString:@"<content>" options:NSCaseInsensitiveSearch];
        if (divRange.location != NSNotFound)
        {
            // Determine "</div>" location according to "<div>" location
            NSRange endDivRange;

            endDivRange.location = divRange.length + divRange.location;
            endDivRange.length   = [responseString length] - endDivRange.location;
            endDivRange = [responseString rangeOfString:@"</content>" options:NSCaseInsensitiveSearch range:endDivRange];

            if (endDivRange.location != NSNotFound)
            {
                // Tags found: retrieve string between them
                divRange.location += divRange.length;
                divRange.length = endDivRange.location - divRange.location;

                result = [responseString substringWithRange:divRange];
            }
            tweet.text = result;
            [_engine setAccessToken:token];
            [_engine sendUpdate:tweet.text];
            [self.parentViewController dismissModalViewControllerAnimated:true];
        }
    }
}

And this is an example of the response:

<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
 <mediaid>50ia96</mediaid>
 <mediaurl>URL GOES HERE</mediaurl>
</rsp>

So ending up, what I want is getting the URL between tags.

Thanks in advance!

Upvotes: 1

Views: 160

Answers (1)

phi
phi

Reputation: 10733

Go to their documentation, and scroll down to the "Handling success and failure for multiple requests in delegate methods" section. They give you three alternatives - most of the times it's enough to set the userInfo dictionary when you make the request, and then just read it in your callbacks and take proper action. Some quick code:

set this when you create and start the request:

request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys: @"firstRequestId", @"id", nil];

and then in your callbacks:

if([[request.userInfo objectForKey:@"id"] isEqualToString:@"firstRequestId"]) {
  // Handle the request with id 'firstRequestId'
}

Upvotes: 1

Related Questions