我一直在使用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);
}}];
这有什么问题? 请帮忙
答案 0 :(得分:0)
您有两个问题:
您在该网址的开头有一个额外的空格。删除多余的空间。
您正在对已经过百分比转义的网址发送stringByAddingPercentEscapesUsingEncoding
。将此来电移至stringByAddingPercentEscapesUsingEncoding
。