2013-09-22 145 views
1

我一直在使用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

確定了它的感謝 –

回答

0

你有兩個問題:

  1. 您在這個網址的開頭的額外空間。刪除額外的空間。

  2. 您打電話給stringByAddingPercentEscapesUsingEncoding已經被百分比轉義的URL。將此呼叫移除至stringByAddingPercentEscapesUsingEncoding

+0

thnaks但我不能夠得到正確的JSON –

+0

你說的正確的Json是什麼意思?你的json響應有特殊字符?點擊瀏覽器上的URL並檢查,其相同。 – thatzprem

+0

@Sunnyshah當我做了這兩個更改時,我得到了一個響應,它被'NSJSONSerialization'成功解析。請澄清你的意思是不是「正確的JSON」。 – Rob