bub*_*drc 6 rest post put ios afnetworking
我正在尝试拨打服务器.GET调用工作正常并返回正确的json,但是当我尝试执行PUT或POST时,服务器返回错误.
我将服务器设置为接收下一条消息:
method POST
curl -X POST -d "number=NUMBER&name=NAME&lat=32.5713&lon=60.3926" http://server.com/users/
method PUT
curl -X PUT -d "number=USER&name=NAME6&lat=-34.5552&lon=32.3333" http://server.com/users/
Run Code Online (Sandbox Code Playgroud)
如何使用这两种方法调用服务器?
小智 10
我会为所有请求创建一个APIClient类,而不是每次发出请求时都创建一个新客户端.
请参阅:https://github.com/AFNetworking/AFNetworking/tree/master/Example/Classes AFTwitterAPIClient.h和AFTwitterAPIClient.m
但根据你的问题.我相信代码看起来像这样.(代码未经测试)
NSURL *url = [NSURL URLWithString:@"http://server.com"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
//depending on what kind of response you expect.. change it if you expect XML
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
@"NUMBER",@"number",
@"NAME",@"name",
@"32.5713",@"lat",
@"60.3926",@"lon",
nil];
[client putPath:@"users" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure");
}];
Run Code Online (Sandbox Code Playgroud)
至于帖子请求..只需使用postPath而不是putPath,它将正常工作.:)
希望我帮了.
问候,
Steve0hh
| 归档时间: |
|
| 查看次数: |
8744 次 |
| 最近记录: |