我正在该应用程序中开发一个应用程序我需要在NSURL中一次传递多个参数 我的代码是
responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//NSURLRequest *request1 = [NSURLRequest requestWithURL:
//[NSURL URLWithString:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=1",strfrom,strto]];
上面的代码我需要动态传递多个参数。可能吗 ? 如果是,那怎么样? 谢谢&问候
答案 0 :(得分:5)
在添加到
之类的URL之前尝试创建单独的字符串 NSSString *strURL=[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo];
然后将此strURL添加到URL
NSURL *url = [NSURL URLWithString:strURL];
最后将其添加到请求中,您的代码错误,您在请求中添加了网址,URL不是字符串,而是URL,因此它应该是requestWithURL
而不是URLWithString
,它应该像这个
NSURLRequest *request = [NSURLRequest requestWithURL:url];
答案 1 :(得分:1)
缺少许多这些答案的一件事是使用[NSString stringByAddingPercentEscapesUsingEncoding:]
来避免在网址中使用无效字符:
NSString *baseURL = [NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo];
NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];