NSURLErrorDomain代码= -1012错误

时间:2013-06-21 16:35:15

标签: nsurlrequest sbjson

我正在尝试登录服务器,但收到错误消息。我使用SBJSON将请求转换为JSON,并将其从JSON转换回字符串,我使用一种方法来进行所有API调用。我四处寻找解决方案,但我找不到任何解决方案。这是我用来登录的方法:

+ (void)requestFromUrl:(NSString *)urlString withType:(NSString *)type withBody:(NSDictionary *)dict
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:type];

    if(dict)
    {
        NSString *jsonString = [dict JSONRepresentation];
        NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:jsonData];
    }

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)
         {
             NSLog(@"Error retrieving data: %@", error.description);
         }
         else
         {
             id result = [data JSONValue];
             NSLog(@"Received responce: %@", result);

             [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedResponce" object:self userInfo:result];
         }
     }];

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

制作本:

    NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];

成:

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

看看是否有帮助。 JSON必须是UTF8编码,而不是ASCII。