How to use Three20 framework to upload multiple images and handle JSON response?
-
26-09-2019 - |
Question
So I have an iPhone application that needs to:
- Post several strings and up to 5 images (stored in memory) to a RoR web application
- Parse the JSON returned that will include several strings and an array of URLs (each representing the location of where the uploaded images can be found on the website).
QUESTIONS:
Can this be done with Three20 (would be nice since I'm using it for other things)? And if so, how?
If it can't be done with Three20 ... how would it be accomplished using ASIHttpRequest? Or maybe something baked into the SDK if that is a better option?
Thanks much
Solution
Unfortunately there isn't a whole lot of tutorials and good documentation for three20 out there on the web ... so here is how I finally got things working:
- (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];
}
Things I still don't understand (or find problematic):
- For a posted file, how can I include both a param name AND a filename?
- What is the purpose of setting request.response = to whatever? I don't get that.
OTHER TIPS
Answering #2:
You need to supply the handler for the response before you send your request, the TTURLJSONResponse
is not the actual response, but it's responsible for handling the response. This is where you'd process the response for your strings and array of URLs.
It's really a protocol called TTURLResponse
that defines the following method for implementation:
/**
* 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;
You chose TTURLJSONResponse
as your handler, which is a straight-forward implementation to look at for help on writing your own.