如何根据 ASIHTTPRequest 执行不同的操作?
Posted
技术标签:
【中文标题】如何根据 ASIHTTPRequest 执行不同的操作?【英文标题】:How to do different actions depending on ASIHTTPRequest? 【发布时间】:2011-05-21 12:11:08 【问题描述】:我正在使用 ASIFormDataRequest 将图像上传到 TwitPic,我得到了响应,从这里一切正常。但是在- (void)requestFinished:(ASIHTTPRequest *)request
上,我已经对 TwitLonger 响应采取了行动(可以正常工作)。现在我将如何根据响应的类型执行不同的操作?我尝试设置一个字符串并与if
进行比较,得到最后的响应,但没有运气。这是我尝试过的方式:
- (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];
这是响应的一个示例:
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>50ia96</mediaid>
<mediaurl>URL GOES HERE</mediaurl>
</rsp>
所以最后,我想要的是获取标签之间的 URL。
提前致谢!
【问题讨论】:
【参考方案1】:Go to their documentation,然后向下滚动到“在委托方法中处理多个请求的成功和失败”部分。它们为您提供了三种选择 - 大多数情况下,在您发出请求时设置 userInfo 字典就足够了,然后在您的回调中读取它并采取适当的措施。一些快速代码:
在创建和启动请求时设置:
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys: @"firstRequestId", @"id", nil];
然后在你的回调中:
if([[request.userInfo objectForKey:@"id"] isEqualToString:@"firstRequestId"])
// Handle the request with id 'firstRequestId'
【讨论】:
感谢您的链接!我认为第一个选项(userInfo 选项)似乎是正确的,但我该怎么做呢?谢谢:)以上是关于如何根据 ASIHTTPRequest 执行不同的操作?的主要内容,如果未能解决你的问题,请参考以下文章
ASIHttpRequest - 如何将参数的 NSDictionary 转换为 url 编码的查询参数字符串?