我正在尝试在 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 天内尝试使用谷歌搜索,但没有任何结果可以使用。
该服务将收到“名称”并返回一个字符串:“你好,名称!”
更新 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.}
请给我一些建议。
谢谢 !