发布请求NSURL / NSURLConnection无法在ios中运行?

时间:2014-10-20 08:27:32

标签: ios iphone post nsurlconnection nsurl

我正在尝试使用一些参数发布帖子请求,但它没有完成,看看

#define kLatestKivaLoansURL [NSURL URLWithString: @"http://url...."]

NSDictionary *params = @{@"medcine": @"xanax", @"lat": @"31.0000",@"long":@"74.0000",@"offset":@"0"};

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:kLatestKivaLoansURL];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];
NSLog(@"%@",request);

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"dataTaskWithRequest error: %@", error);
    }

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
        if (statusCode != 200) {
            NSLog(@"Expected responseCode == 200; received %ld", (long)statusCode);
        }
    }
     }];
[task resume];
   - (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
  NSMutableArray *parameterArray = [NSMutableArray array];

[paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
    NSString *param = [NSString stringWithFormat:@"%@=%@", key, [self percentEscapeString:obj]];
    [parameterArray addObject:param];
}];

NSString *string = [parameterArray componentsJoinedByString:@"&"];
NSLog(@"%@",string);
return [string dataUsingEncoding:NSUTF8StringEncoding];

}

 - (NSString *)percentEscapeString:(NSString *)string
  {
     NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                             (CFStringRef)string,
                                                                             (CFStringRef)@" ",
                                                                             (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                 kCFStringEncodingUTF8));
     NSLog(@"%@",result);
     return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];


 }

错误

  APP[2866:154303] dataTaskWithRequest error: Error Domain=NSURLErrorDomain Code=-1003 "The operation couldn’t be completed. (NSURLErrorDomain error -1003.)" UserInfo=0x7874ef60 {NSErrorFailingURLStringKey=http://URL..., _kCFStreamErrorCodeKey=8, 
 NSErrorFailingURLKey=http://URL...,   _kCFStreamErrorDomainKey=12, NSUnderlyingError=0x78773e70 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1003.)"}

2 个答案:

答案 0 :(得分:0)

尝试:替换以下方法。

- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{

NSError *error =nil;
 return  [NSJSONSerialization dataWithJSONObject:paramDictionary
                                                       options:0
                                                         error:&error];
}

答案 1 :(得分:0)

此链接很好地解释了错误代码AppleDevDoc。以下是关于此错误的说法:

kCFURLErrorCannotFindHost  = -1003

我认为域名是错误的。