我正在尝试调用服务器。 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/
这两种方法如何调用服务器?
最佳答案
我会为所有请求创建一个 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");
}];
至于发布请求.. 只需使用 postPath 而不是 putPath 就可以了。 :)
希望我帮到了你。
关于ios - POST 和 PUT 请求 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974241/