AFNetworking使用JSON请求WCF REST服务

4

我正在使用AFNetworking处理我的REST服务(WCF)。以下是代码:

  NSDictionary *userName = [NSDictionary dictionaryWithObject:@"user" forKey:@"UserNameOrEmail"];
  NSDictionary *pass = [NSDictionary dictionaryWithObject:@"123" forKey:@"Password"];
  NSArray *credentials = [NSArray arrayWithObjects:userName,pass,nil];
  NSDictionary *params = [NSDictionary dictionaryWithObject:credentials forKey:@"request"];


  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
  [NSURL URLWithString:@"http://server"]];

  [client postPath:@"/ProfileWebService.svc/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Response: %@", text);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", [error localizedDescription]);
}];

但是我遇到了400 HTTP错误。

使用HTML和AJAX,代码如下:

        $(document).ready(function () {

            $("#login_call").click(function () {
                $.ajax({
                    type: 'POST',
                    url: 'http://server/ProfileWebService.svc/login',
                    contentType : 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({request: {UserNameOrEmail: $('#login_username').val(), Password: $('#login_password').val()}}),
                    success: function (data) {
                        $('#login_result').val('Code: ' + data.LoginResult.Code + '\nFault String: ' + data.LoginResult.FaultString);
                    }
                });
            });

        });

并且运行良好。

参数有什么问题。


更详细的AFNetworking文档研究给了我答案,我只需要添加一行代码: client.parameterEncoding = AFJSONParameterEncoding;就这样! - matr0sk1n
你能把你的评论发布为答案并接受吗? - carlosfigueira
我没有足够的声望,在我提出问题后只有6个小时。 - matr0sk1n
1个回答

3
更详细的AFNetworking文档学习和在Stackoverflow上的搜索为我提供了一个解决方案
1. 请求参数应该像这样:
  NSDictionary *params =
        [NSDictionary dictionaryWithObjectsAndKeys:
           [NSDictionary dictionaryWithObjectsAndKeys:
                           username.text, @"UserNameOrEmail",
                           password.text, @"Password",
                           nil],
        @"request",
        nil];

2.创建AFHTTPClient

  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
        [NSURL URLWithString:@"http://server"]];

3. 当我将JSON作为参数发送时,我需要添加:

  client.parameterEncoding = AFJSONParameterEncoding;

我做完这个操作后解决了404错误,但是无法处理JSON响应,我不知道为什么。解决方法如下:

4.创建一个请求:

  NSMutableURLRequest *request = 
  [client requestWithMethod:@"POST" path:@"/ProfileWebService.svc/login" parameters:params];

5.创建操作:

  AFJSONRequestOperation *operation =
  [AFJSONRequestOperation JSONRequestOperationWithRequest:request
  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
  {
     NSLog(@"JSON: %@", [JSON valueForKeyPath:@"LoginResult"]);
     NSLog(@"Code: %@", [[[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"Code"] stringValue]);
     NSLog(@"FaultString: %@", [[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"FaultString"]);
  }
  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
  {
     NSLog(@"error opening connection");
  }];

6. 开始操作:

  [operation start];

希望一些AFNetworking的初学者能从这篇文章中受益。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接