How to use Three20 structure to load multiple images and process JSON response?

So, I have an iPhone app that needs to:

  • Print multiple lines and up to 5 images (stored in memory) in a RoR web application.
  • Consider the returned JSON, which will contain several lines and an array of URLs (each of which represents a location where uploaded images can be found on the website).

QUESTIONS:

  • Is it possible to do this with Three20 (it would be nice, because I use it for other things)? And if so, how?

  • If this cannot be done using Three20 ... how can this be done using ASIHttpRequest? Or maybe something is baked in the SDK, if this is the best option?

thanks a lot

+4
source share
2 answers

Unfortunately, there aren't many tutorials and good documentation for the three20 on the Internet ... so here's how I finally got the job:

- (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 do not understand (or find problematic):

  • For a published file, how do I include the parameter name AND file name?
  • What is the purpose of setting request.response = on what? I do not understand this.
+4
source

Answer # 2: You need to provide a handler for the response before sending the request, TTURLJSONResponse not the actual answer, but it is responsible for processing the response. Here you process the response for your strings and an array of URLs.

This is 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 have chosen TTURLJSONResponse as your handler, which is a straightforward implementation to look for help writing your own.

+1
source

Source: https://habr.com/ru/post/1309509/


All Articles