我有一个JSON请求和一些参数......我需要在其中一个参数中传递一个数组......怎么样?
有我当前的请求......我有3个参数:纬度,经度和数组:数组必须是ids数组(int)
数组:NSArray arrayIds = [NSArrayWithObjects:@"1", @"2", @"3", nil];
-(void) listarAtracoesBusca
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
self.listaAtracoes = [[NSMutableArray alloc]init];
self.indiceAtracaoAtual = 0;
NSString *urlStr = @"";
float latitude = 0.0;
float longitude = 0.0;
if(appDelegate.latitudeAtual)
latitude = appDelegate.latitudeAtual;
if(appDelegate.longitudeAtual)
longitude = appDelegate.longitudeAtual;
[self validarCamposBusca];
urlStr = [NSString stringWithFormat:@"https://host:3000/api/ params?latitude=%f&longitude=%f&array=", latitude, longitude, arrayIds];
NSURL *URL = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setValue:@"haadushdiuashud" forHTTPHeaderField:@"X-User-Authorization"];
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", @"user", @"pass"];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
request.HTTPMethod = @"GET";
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//Do Stuff
if (data.length > 0 && error == nil)
{
}
else {
NSLog(@"erro: %@", error.description);
}
}];
}
答案 0 :(得分:1)
如果您正在尝试实现查询URL之类的内容,则可以使用NSURLQueryItem来表示URL的查询部分的单个键/值对。
NSURLComponents *components = [NSURLComponents componentsWithString:@"http://stackoverflow.com"];
NSURLQueryItem *search = [NSURLQueryItem queryItemWithName:@"q" value:@"ios"];
NSURLQueryItem *count = [NSURLQueryItem queryItemWithName:@"count" value:@"10"];
components.queryItems = @[ search, count ];
NSURL *url = components.URL; // http://stackoverflow.com?q=ios&count=10
否则,您需要使用NSJSONSerialization将JSON作为请求的主体传递。有关详细信息,请参阅here。