使用AFHTTPRequestOperationManager的HTTP请求

时间:2014-03-06 05:46:21

标签: php ios http-post afnetworking-2 afhttprequestoperation

嗨,我是AFNetworking 2.0的新手,我需要使用AFHTTPRequestOperationManager连接我的php文件,但如果我得到响应,它只返回null。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFImageResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:contactName forKey:kContactNameKey];
    [parameters setObject:mailTo forKey:kMailToKey];
    [parameters setObject:mailCc forKey:kMailCcKey];
    [parameters setObject:category forKey:kCategoryKey];


    [manager POST:@"http://www.sample.com/sample/sample.php" parameters:parameters
          success:^(AFHTTPRequestOperation *operation,id responseObject){

              NSLog(@"Success %@",responseObject);
    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error){
              NSLog(@"%@",error);
          }];

btw这是我的php文件中的一个示例行

$EmailTo=mosGetParam($_REQUEST, 'mailTo', '');

$EmailCC=mosGetParam($_REQUEST, 'mailCc', '');

$fromName=mosGetParam($_REQUEST, 'fromName', '');

$deviceId=mosGetParam($_REQUEST, 'deviceId', '');

$appVersion=mosGetParam($_REQUEST, 'appVersion', '1.0');

$osVersion=mosGetParam($_REQUEST, 'osVersion', '');

我希望你能帮助我..提前致谢:)

1 个答案:

答案 0 :(得分:1)

我找到了我的解决方案,我唯一的问题是这一行

manager.responseSerializer = [AFImageResponseSerializer serializer];

所以我把它改成了http

manager.responseSerializer = [AFHTTPRequestSerializer serializer];

我会将responseObject转换为utf8以获取它的字符串。

success:^(AFHTTPRequestOperation *operation,id responseObject){

              NSString *string = [[NString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSLog(@"Success: %@",string);
    }