2012-02-06 118 views
4

如何將GET參數添加到ASIHttpRequest?如何將GET參數添加到ASIHttpRequest?

我想以編程方式從http://mysite.com/serverhttp://mysite.com/server?a=1&b=2

我有我的所有參數作爲NSDictionary對象中的鍵值對。

感謝

+1

這已經在這裏討論: http://stackoverflow.com/questions/718429/creating-url-query-parameters-from-nsdictionary -objects-in-objectivec 希望這有助於! – Goeran 2012-02-06 12:02:06

回答

5

使用字符串格式像

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil]; 

NSMutableString *prams = [[NSMutableString alloc] init]; 

for (id keys in dict) { 
    [prams appendFormat:@"%@=%@&",keys,[dict objectForKey:keys]]; 
} 
NSString *removeLastChar = [prams substringWithRange:NSMakeRange(0, [prams length]-1)]; 
NSString *urlString = [NSString stringWithFormat:@"http://mysite.com/server?%@",removeLastChar]; 

NSLog(@"urlString %@",urlString); 
+0

它不會轉義特殊字符。 – jweyrich 2013-09-23 21:28:33