【问题标题】:deal with special characters in NSURL处理 NSURL 中的特殊字符
【发布时间】:2014-11-24 01:17:15
【问题描述】:

我有这个代码到图像的 url,我的 url 没有什么问题,它包含 \u 和 xcode 认为它是特殊字符所以我通过在我的 url 中添加 \u 来逃避它 但是当我通过它时

fullPath = [NSString stringWithFormat:@"http:\\www.school-link.net\\uploads\\%@",image_url];
    NSLog(@"file path  %@",fullPath);
    //i try escape space by this code it dosnt work 
   //fullPath = [fullPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc]initWithString:fullPath];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];



 AFHTTPRequestOperation *posterOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    posterOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [posterOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        image_view.image = responseObject;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image request failed with error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:posterOperation];
    [posterOperation start];

它给我错误,任何想法 谢谢大家

图像请求失败并出现错误:Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x8f9f900 {NSUnderlyingError=0x9a94cf0 "bad URL", NSLocalizedDescription=bad URL}

【问题讨论】:

    标签: ios afnetworking nsurl


    【解决方案1】:

    您的网址格式错误。现在你有:

    http:\\www.school-link.net\\uploads\\%@
    

    那些反斜杠应该是正斜杠。例如:

    http://www.school-link.net/uploads/%@
    

    现在您可以将image_url 附加到URL,并且假设image_url 不会导致URL 格式错误,您的请求将通过。

    【讨论】:

      【解决方案2】:

      使用 NSUTF8String 编码。它将解决问题。例如

      NSString *strURL = [NSString stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL];
      

      在您的代码中注释了使用它的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        • 2012-09-18
        • 2017-07-12
        相关资源
        最近更新 更多