我正在使用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。
提前致谢!
答案 0 :(得分: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'
}