-1

我在服务器上发出请求,在 URL 中有空格

http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd¶ms[]=Saudi%20Arab¶ms[]=all

我收到错误错误 URL 所以,

我用了

   downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

但由于这个原因,我得到了奇怪的 URL

 http://sdsdsdsdsdsd.com/api/api.php?func=showAllsdsd&params5262721536=Saudi              0X0P+0rabia&params5 8288=All

我也在使用

  downloadURL= [downloadURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

但又像奇怪的网址

     http://xxxxxxxxx.com/api/api.php?func=showsdsdsd&params[]=Saudi 0X1.240DC0D824P-807rabia&params[]=All

请帮助我如何解决这个问题

编辑粘贴大部分代码

   PropertyXMLDownloader *download=[[PropertyXMLDownloader alloc]init];
  [download setDelegate:self];
   firstAppDelegate *del=(firstAppDelegate *)[[UIApplication sharedApplication]delegate];
   NSLog(@"Country is %@",del.country);
   NSLog(@"State is %@",del.state);
//  del.country=[del.country stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

   NSString *downloadURL=[NSString stringWithFormat:@"http://xxxxxxxx.com/api/api.php?func=showAll&params[]=Saudi Arabia&params[]=%@",@"all"];
      // downloadURL= [downloadURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
       //downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

   NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@" "];
   downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant]    componentsJoinedByString:@"%20"];


    NSLog(downloadURL); 
    [download startDownloading:downloadURL];
4

3 回答 3

1

试试这个。

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@" "];
downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant]   componentsJoinedByString:@"%20"];
于 2012-08-04T06:55:43.043 回答
1

也许 %20 被视为数据参数,如 %@ 或 %g。尝试使用定义 NSString

NSString *urlString = [NSString stringWithFormat:@"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd&params[]=Saudi%20Arab&params[]=all"];

你会看到一个警告。通过在百分号前面添加另一个来“转义”百分号:

NSString *urlString = [NSString stringWithFormat:@"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd&params[]=Saudi**%**%20Arab&params[]=all"];

警告消失了。

于 2012-08-04T07:41:53.393 回答
0

你的问题是这样的:

NSLog(downloadURL); 

尝试将其替换为:

NSLog(@"%@", downloadURL); 

一切都会奏效。

您可以使用stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding并忘记所有解决方法。

(解释:由于downloadURL包含 % 符号,它不能很好地与 NSLog 配合使用,它需要一个格式字符串作为它的第一个参数,其中 % 符号标识要替换的占位符)。

于 2012-08-04T06:47:13.003 回答