NSURLResponse返回nil?

NSURLResponse返回nil?

问题描述:

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

NSString *[email protected]" 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); 

     }}]; 

这是什么问题? 帮助,请

+0

确定了它的感谢 –

你有两个问题:

  1. 您在这个网址的开头的额外空间。删除额外的空间。

  2. 您打电话给stringByAddingPercentEscapesUsingEncoding已经被百分比转义的URL。将此呼叫移除至stringByAddingPercentEscapesUsingEncoding

+0

thnaks但我不能够得到正确的JSON –

+0

你说的正确的Json是什么意思?你的json响应有特殊字符?点击浏览器上的URL并检查,其相同。 – thatzprem

+0

@Sunnyshah当我做了这两个更改时,我得到了一个响应,它被'NSJSONSerialization'成功解析。请澄清你的意思是不是“正确的JSON”。 – Rob