我試圖在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.}
請給我一些建議。
謝謝!