【问题标题】:NSString is considered as null when converted in URLNSString 在 URL 中转换时被认为是 null
【发布时间】:2014-01-23 14:07:09
【问题描述】:

我正在尝试调用一个 drupal 搜索网络服务,但是当我调用它时,它说请求为空。

我在我的代码中输入了一些日志,发现 NSString 很好,但是当我定义我的 NSURL 时,NSString 被视为空...有人可以解释一下问题是什么吗?

以下是我的代码:

- (void)FindProductsByKeyword:(NSString *)keyword :(void(^)(NSArray *produits))success
{
    __block NSArray *produits = [[NSArray alloc]init];

    NSString *fullUrl = [NSString stringWithFormat:@"%@%@?filter[title]=\%%%@\%%&filter_op[title]=LIKE", WEBSERVICES_URL, PRODUCT_DISPLAY_URL, keyword];
    NSLog(@"Appel de : %@", fullUrl);
    NSURL *URLFormatted = [NSURL URLWithString:fullUrl];
    NSLog(@"Appel de : %@", URLFormatted);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:fullUrl]];
    NSLog(@"Appel de : %@", request);

    [request setHTTPShouldHandleCookies:NO];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:
     ^(NSURLResponse * response, NSData * data, NSError * error)
     {
         NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response;
         NSLog(@"status code : %d", httpResponse.statusCode);
         if(httpResponse.statusCode == 200) {
             NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
             produits = [[WebservicesService getInstance]ExtractListProduct:jsonData];
             NSLog(@"Il y a %d résultats pour la recherche", produits.count);
         }
         success(produits);
     }];
}

这是我的日志显示的内容:

2014-01-03 17:03:51.852 mcommerce_ipad[8649:a0b] Appel de : http://domain-name.com/rest/product-display.json?filter[title]=%test%&filter_op[title]=LIKE

2014-01-03 17:03:51.852 mcommerce_ipad[8649:a0b] Appel de : (null)

2014-01-03 17:03:51.853 mcommerce_ipad[8649:a0b] Appel de : <NSMutableURLRequest: 0x8a3c9c0> { URL: (null) }

2014-01-03 17:03:51.895 mcommerce_ipad[8649:a0b] status code : 0

当我直接尝试我的 NSString 时,萤火虫最终会返回什么。

200 OK

17   Object { vid="17",   uid="0",   title="Ethylotest chimique à usage unique", more...}   

139 Object { vid="139", uid="16", title="Produit de test", more...}

142 Object { vid="142", uid="16", title="Test surface", more...}

这表明请求是正确的并且应该返回一些东西,问题来自从 NSString 到 NSURL 的转换,但我找不到它......所以请成为我的救星!

【问题讨论】:

  • 尝试转义特殊字符:[NSURL URLWithString:[fullUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
  • 谢谢你,完美地完成了这项工作!我以前从未写过这种类型的请求,现在多亏了你,我才能正确地完成它。

标签: ios null nsstring nsurl nsurlrequest


【解决方案1】:

正如我在评论中所说,您需要转义特殊字符,否则 URLWithString 将返回 null。

为此,请使用:

[NSURL URLWithString:[fullUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

【讨论】:

    【解决方案2】:

    在将字符串转换为 URL 之前,您需要对字符串的参数部分进行 URL 编码。它可以在浏览器中工作,因为浏览器会在发送请求之前为您完成。

    具体需要修改%符号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2023-03-11
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多