因此,经过大量研究,我想出了如何NSDictionary
从我的 iOS 6 客户端发送一个到我的 Rails 服务器。我用过AFNetworking
。下面是发送 JSON 的客户端代码。但是,我不确定这是否是异步调用,因为我看不到在哪里使用了操作队列。如果不是,那么如何将其变为异步调用?
-(void)sendEntryToServerAsJSON:(EntryParent*)_entryToBeSaved
{
NSDictionary* dictToBeSerialized = [_entryToBeSaved convertEntryParentObjToDict];
[[appAPIClient sharedClient]postPath:@"entries.json"
parameters:dictToBeSerialized
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully sent JSON %@", [responseObject description]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Could not send JSON %@", [error description]);
}];
}
这是我的 AFHTTPClient 实现
+ (appAPIClient *)sharedClient
{
NSLog(@"Inside appAPIClient sharedclient ");
static appAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc]
initWithBaseURL:[NSURL URLWithString:URL_STR]];
});
return _sharedClient;
}
//==============================================================================
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (!self)
{
return nil;
}
NSLog(@"init with base url - appAPIClient");
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
return self;
}
我将不胜感激任何反馈以改进此代码并使其异步。