0

I have a JSON Request and some parameter...I need to pass an array in one of these parameters...How?

There's my current request...I have 3 parameters: latitude, longitude and Array: Array must be an array of ids (int)

Array: 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);
                               }
                           }];
}
6
  • What are the values you have in array? What is the URL you are trying to form? Commented Oct 26, 2015 at 10:48
  • Any array: NSArray arrayIds = [NSArrayWithObjects:@"1", @"2", @"3", nil]; for example Commented Oct 26, 2015 at 10:50
  • Are you passing this JSON as a body of this request? Have you looked at NSJSONSerialization? Commented Oct 26, 2015 at 10:51
  • Ok. What is your exact URL you are trying to form? Commented Oct 26, 2015 at 10:51
  • The url is available only locally Commented Oct 26, 2015 at 10:52

1 Answer 1

1

If you are trying to achieve some thing like query url then you can use NSURLQueryItem that will represent the single key/value pair for query portion of 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

Otherwise you need to pass the JSON as body of your request using NSJSONSerialization. Refer here for more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.