【问题标题】:Consume a web service with AFNetworking 2使用 AFNetworking 2 使用 Web 服务
【发布时间】:2013-11-04 07:14:37
【问题描述】:

我正在尝试在 iOS 项目中使用网络服务。我为此使用 AFNetworking。但是当前版本是 2.x,但我发现只有 1.x 版本的示例代码:

- (IBAction)loginButtonPressed {        
    NSURL *baseURL = [NSURL URLWithString:@"https://localhost/ws/"];


    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
    [httpClient defaultValueForHeader:@"Accept"];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"tester", "nameField", 
                            nil];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
          path:@"https://localhost/ws/hello" parameters:params];

    //Add your request object to an AFHTTPRequestOperation
    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] 
                                      initWithRequest:request] autorelease];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:
      ^(AFHTTPRequestOperation *operation, 
      id responseObject) {
        NSString *response = [operation responseString];
        NSLog(@"response: [%@]",response);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", [operation error]);
    }];

    //call start on your request operation
    [operation start];
    [httpClient release];
}

我不知道如何将“AFHTTPClient”转换为“AFHTTPRequestOperationManager”或“AFHTTPRequestOperation”

我在 2 天内尝试使用谷歌搜索,但没有任何可用的结果。

服务将收到'name'并返回一个字符串:'hello, name!'

更新 1

我尝试了下面的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"name": @"tester"}; [manager POST:@"http://localhost/wp/?wsdl" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];

并得到错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8ae1f00 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

请给我一些建议。

谢谢!

【问题讨论】:

    标签: objective-c web-services afnetworking-2


    【解决方案1】:

    我修好了!

    问题:

    服务器正在使用 nuSOAP。我更改为另一个返回 JSON 的 Web 服务,现在客户端可以完美地使用 Web 服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多