与AFNetworking一起使用Web服务2
我试图在iOS项目中使用Web服务。我使用AFNetworking。不过目前的版本是2.x的,但我发现唯一的例子代码的1.x版本:与AFNetworking一起使用Web服务2
- (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”
我在两天内尝试了Google搜索,但没有任何结果可以使用。
该服务将收到'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); }];
,并得到eror:
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.}
请给我一些建议。
谢谢!
我修好了!
问题:
Server使用的NuSOAP。我更改为另一个返回JSON的Web服务,现在客户端可以完美地使用Web服务。