2011-05-21 63 views
1

我正在使用ASIFormDataRequest將圖片上傳到TwitPic,並且我得到了一個響應,從這裏一切正常。但在- (void)requestFinished:(ASIHTTPRequest *)request我已經有一個TwitLonger響應的動作(正常工作)。現在,我將如何根據響應類型來執行不同的操作?我嘗試設置一個字符串並與if進行比較,得到響應的最後一個東西,但沒有運氣。這是我試過的方式:如何根據ASIHTTPRequest執行不同的操作?

- (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' 
} 
+0

感謝您的聯繫!我認爲第一個選項(用戶信息一)似乎是正確的,但我該怎麼做呢?謝謝:) – pmerino 2011-05-21 13:07:15

+0

檢查我編輯的答案 – phi 2011-05-21 13:16:21

+0

非常感謝!它的工作:D – pmerino 2011-05-21 13:19:13

相關問題