我正在尝试调用服务器。 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 类,而不是每次发出请求时创建一个新客户端。
See : https://github.com/AFNetworking/AFNetworking/tree/master/Example/ClassesAFTwitterAPIClient.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");
}];
至于post请求..只需使用postPath而不是putPath,它就可以正常工作。 :)
希望我有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)