2010-05-12 70 views
4

所以我需要一個iPhone應用程序:如何使用Three20框架上傳多個圖片並處理JSON響應?

  1. 後幾個字符串和最多5張(存儲在內存中)的回報率Web應用程序
  2. 解析JSON返回,其中將包括幾個字符串和一組網址(每個網址都代表可以在網站上找到上傳圖片的位置)。

問題:

  1. 可以這樣用Three20做(因爲我使用的是其他東西就好了)?如果是這樣,怎麼樣?

  2. 如果使用Three20無法完成......使用ASIHttpRequest將如何實現?或者,如果這是一個更好的選擇,也許可以將東西放入SDK中?

感謝很多

回答

4

不幸的是,不是一大堆的教程和three20良好的文檔在那裏在網絡上的...所以這裏是如何我終於得到的東西的工作:

- (void) sendToWebsite { 

    NSString* url = [[NSString stringWithFormat:kRequestURLPath, self.entityId] stringByAppendingString:@".json"] ; 

    // Prep. the request 
    TTURLRequest* request = [TTURLRequest requestWithURL: url delegate: self]; 
    request.httpMethod = @"POST"; 
    request.cachePolicy = TTURLRequestCachePolicyNoCache; 

    // Response will be JSON ... BUT WHY DO I NEED TO DO THIS HERE??? 
    request.response = [[[TTURLJSONResponse alloc] init] autorelease]; 

    // Set a header value 
    [request setValue:[[UIDevice currentDevice] uniqueIdentifier] forHTTPHeaderField:@"Device-UID"]; 

    // Post a string 
    [request.parameters setObject:self.entity_title forKey:@"entity_title"]; 

    // Post some images 
     for (int i = 0; i < [self.photos count]; i++) { 
     // IS IT POSSIBLE TO ADD A PARAM NAME SO I CAN LOOK FOR THE SAME NAME 
     // IN THE WEB APPLICATION REGARDLESS OF FILENAME??? 
     [request addFile:UIImagePNGRepresentation([self.winnerImages objectAtIndex:i]) 
       mimeType:@"image/png" 
       fileName:[NSString stringWithFormat:@"photo_%i.png", i]]; 
    } 

     // You rails guys will know what this is for 
     [request.parameters setObject:@"put" forKey:@"_method"]; 

     // Send the request 
    [request sendSynchronously]; 

} 

事情我還是不明白(或覺得有問題):

  1. 對於發佈文件,我怎麼能既包括對名字和文件名?
  2. 設置request.response =的目的是什麼?我不明白。
1

回答#2: 您需要提供處理程序爲響應您發送請求之前,該TTURLJSONResponse是不實際的響應,但它是負責處理響應。這是你處理你的字符串和URL數組的響應的地方。

這真是一個協議稱爲TTURLResponse,它定義了實現下面的方法:

/** 
* Processes the data from a successful request and determines if it is valid. 
* 
* If the data is not valid, return an error. The data will not be cached if there is an error. 
* 
* @param request The request this response is bound to. 
* @param response The response object, useful for getting the status code. 
* @param data  The data received from the TTURLRequest. 
* @return NSError if there was an error parsing the data. nil otherwise. 
* 
* @required 
*/ 
- (NSError*)request:(TTURLRequest*)request 
      processResponse:(NSHTTPURLResponse*)response 
      data:(id)data; 

您選擇TTURLJSONResponse爲您的處理程序,這是一個直觀的實現來看待的幫助編寫自己的。