我创建了一个简单的Rails应用程序:
$ rails new server --database=postgresql
$ rails generate scaffold contact name address city zip phone
$ git init
$ git add .
$ git commit -m "initial"
$ rake db:create
$ rake db:migrate
然后我使用AFNetworking框架创建了一个iOS客户端。我以这种方式向数据库添加了新记录:
+ (void) add: (Contact *)contact block:(void (^)(NSError *error))block{
NSDictionary *fields = @{@"name": contact.name,
@"address": contact.address,
@"city": contact.city,
@"zip": contact.zip,
@"phone": contact.phone
};
NSDictionary *params = @{@"contact":fields,
@"commit":@"add Contact"};
NSString *urlString = @"http://0.0.0.0:300/contacts/";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//.......
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//......
}];
}
然后我就这样列出了记录:
+ (void)list:(void (^)(NSArray *contacts, NSError *error))block{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *url = @"http://0.0.0.0:300/contacts/";
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *mutableContacts = [NSMutableArray arrayWithCapacity:[responseObject count]];
for(NSDictionary *item in responseObject) {
Contact *contact = [[Contact alloc] initWithAttributes:item];
[mutableContacts addObject:contact];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ....
}];
}
当我尝试使用此代码更新记录时:
+ (void) update: (Contact *)contact block:(void (^)(NSError *error))block{
NSDictionary *fields = @{@"name": contact.name,
@"address": contact.address,
@"city": contact.city,
@"zip": contact.zip,
@"phone": contact.phone
};
NSDictionary *params = @{@"contact":fields,
@"commit":@"Update Contact"};
// url example ==>> http://0.0.0.0:3000/contact/2.json
NSString *urlString = contact.url;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager PUT:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (block) {
block(nil);
}
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Response: %@", [operation description]) ;
if (block) {
block(error);
}
NSLog(@"Error: %@", error);
}];
}
我收到了这个错误:
Started PUT "/contacts/2.json" for 192.168.0.3 at 2015-02-03 16:16:05 -0200
Error occurred while parsing request parameters.
Contents:
commit=Update%20Contact&contact[address]=xxxxxxx&contact[city]=xxxxxxx&contact[name]=BLA%20BLA%20BLA&contact[phone]=xxxxxxx&contact[zip]=xxxxxxx
ActionDispatch::ParamsParser::ParseError (795: unexpected token at 'commit=Update%20Contact&contact[address]=xxxxxxx&contact[city]=xxxxxxx&contact[name]=BLA%20BLA%20BLA&contact[phone]=xxxxxxx&contact[zip]=xxxxxxx'):
actionpack (4.1.7) lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters'
actionpack (4.1.7) lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters'
actionpack (4.1.7) lib/action_dispatch/middleware/params_parser.rb:23:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.7) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.7) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.7) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.7) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.7) lib/rails/engine.rb:514:in `call'
railties (4.1.7) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/gazollajunior/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/gazollajunior/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/gazollajunior/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
我在List中遵循相同的模式并添加方法,但更新不起作用。
我做错了什么?
答案 0 :(得分:1)
这根本不像json:
commit=Update%20Contact&contact[address]=xxxxxxx&contact[city]=xxxxxxx&contact[name]=BLA%20BLA%20BLA&contact[phone]=xxxxxxx&contact[zip]=xxxxxxx
这看起来不对:
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
...
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
将AFHTTPRequestSerializer替换为AFJSONRequestSerializer