1

我一直在使用 NSURLConnection 的 sendAsynchronousRequest:queue:completionHandler: 方法。但是,我无法得到回应。每次 httpResponse 返回 nil。

NSString *str=@" http://maps.googleapis.com/maps/api/directions/json?origin=Bapunagar,%20Ahmedabad,%20Gujarat&destination=CG%20Road,%20Sattar%20Taluka%20Society,%20Usman%20Pura,%20Ahmedabad,%20Gujarat&sensor=false&units=metric&mode=transit&departure_time=1343605500&language=ar";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * url = [NSURL URLWithString:str];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];

    [NSURLConnection
     sendAsynchronousRequest:request
     queue:[[NSOperationQueue alloc] init]
     completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         NSLog(@"%@",httpResponse);
         if ([data length] >0 && error == nil && [httpResponse statusCode] == 200)
         {
             NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
             NSLog(@"%@",json);

         }}];

这有什么问题?请帮忙

4

1 回答 1

0

你有两个问题:

  1. 您在该 URL 的开头有一个额外的空格。删除多余的空间。

  2. 您正在调用stringByAddingPercentEscapesUsingEncoding已被百分比转义的 URL。删除对 的调用stringByAddingPercentEscapesUsingEncoding

于 2013-09-22T06:14:52.080 回答