我正在尝试在iOS项目中使用Web服务。我使用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.}
请给我一些建议。
谢谢!
答案 0 :(得分:0)
我修好了!
<强>问题:强>
服务器正在使用nuSOAP。我改为另一个返回JSON的web服务,现在客户端可以完全使用Web服务。