AFNetworking JSON RESTful服务参数

时间:2015-06-13 11:18:22

标签: ios json xcode afnetworking afnetworking-2

我是使用AFNetworking的新手,我还缺少一些调用API的内容。

这是代码(大部分是AF *示例):

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
AFJSONRequestSerializer *reqSerializer = [AFJSONRequestSerializer serializer];
[reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = reqSerializer;

AFJSONResponseSerializer *resSerializer = [AFJSONResponseSerializer serializer];
[resSerializer setReadingOptions:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers];
resSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
manager.responseSerializer = resSerializer;

NSString *URLString =[NSString stringWithFormat:@"%@/users/userLogin", BaseURLString];
NSDictionary *params = @{
                         @"userName": self.txtUser.text,
                         @"passWord": self.txtPass.text
                         };

[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

调用代码的URL是:     http://localhost:8000/users/userLogin

但正确的参数应该是:     http://localhost:8000/users/userLogin/JOHN/MYPASS

最后我得到了"请求失败:未找到(404)"

测试AFNetworking的脏方法:

NSString *URLString = [NSString stringWithFormat:@"%@/users/userLogin/%@/%@", BaseURLString, self.txtUser.text, self.txtPass.text];

它有效:p

知道出了什么问题吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

确保您的资源路径正确。当URL路径无效或服务器找不到请求的资源时,将生成404错误。如果你传递post paramater,那么服务器应该只处理post参数。网址应该是这样的'http://localhost:8000/users/userLogin/'