所以我有一个需要的iPhone应用程序:
问题:
这可以用Three20完成(因为我用它来做其他事情会很好吗?)如果是这样,怎么样?
如果使用Three20无法完成...如何使用ASIHttpRequest完成?或者,如果这是一个更好的选择,可能会在SDK中加入一些内容吗?
非常感谢
答案 0 :(得分:4)
不幸的是,网上没有很多关于三个教程的教程和良好的文档......所以这就是我最终如何工作:
- (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 :(得分: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
作为处理程序,这是一个直接的实现,可以帮助您自己编写。