AFNetworking POST请求无效?

时间:2014-10-14 12:50:30

标签: ios afnetworking-2

这个问题已在很多地方得到解答,但没有解决方案对我有用!我正在使用AFNetworking的代码,如下所示

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFJSONResponseSerializer serializer];
NSDictionary *parameters=@{@"Key1":@"Value1",@"Key2":@"Value2"};
//    NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"https://www.MyURL.com/index.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

错误:

Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7a2a03b0 {NSErrorFailingURLKey=https://www.MyURL.com/index.php, NSErrorFailingURLStringKey=https://www.MyURL.com/index.php}

我对上述请求没有运气。

我不知道POST请求中的代码有什么问题,GET请求在AFNetworking中正常运行。

1 个答案:

答案 0 :(得分:1)

试试这个:

 NSDictionary *dictParameters = parameter here

 //create url
 NSString *strURL = [NSString stringWithFormat:@"url here"];
 NSLog(@"loginurl : %@",strURL);

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 manager.responseSerializer = [AFHTTPResponseSerializer serializer];

 AFHTTPRequestOperation *apiRequest = [manager POST:strURL parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
    NSData *jsonData = (NSData *)responseObject;

    NSError * parsedError = nil;

    id *value = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&parsedError];
    if (parsedError == nil)
    {
       //Successfull
    }
    else
    {
       NSLog(@"wrong while parsing json data");
    }
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
      NSLog(@"Error : %@",[error description]);
 }];

 //start request right now
 [apiRequest start];

编辑:刚刚格式化