如何在AFHTTPRequestOperationManager中发布json数据和响应json

时间:2014-06-24 09:27:49

标签: objective-c afnetworking json-rpc

我使用AFHTTPRequestOperationManager从服务中查询数据,但我希望将POST json数据参数的设置数据用于服务。

感谢您的回答。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];

[operationManager POST:@"yourUrl" parameters:yourParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

答案 1 :(得分:0)

谢谢,这个问题解决了。

示例代码:

NSMutableURLRequest *requestHTTP = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:my_url] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

[requestHTTP setHTTPMethod:@"POST"];
[requestHTTP setValue:AUTHORIZATION_VALUE forHTTPHeaderField:@"Authorization"];
[requestHTTP setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestHTTP setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:requestHTTP];
op.responseSerializer = [AFJSONResponseSerializer serializer];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"JSON responseObject: %@ ",responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", [error localizedDescription]);

}];

[op start];