在我的应用程序中,我需要通过 NSURL 向 google 方向服务发出 json 请求。
当 URL 是静态的时,我能够获得 JSON 响应。
NSURL *googleMapsURL=[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=SanFrancisco,CA&destination=Dallas,TX&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false"];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
但是,当我动态指定源站和目标站时,它不起作用。
NSString *urlString=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false",sourceStation, destinationStation];
NSURL *googleMapsURL=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
它正在生成以下错误。
2012-05-08 03:30:15.960 SupplyTrackerApp2[3079:16403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x19a7052 0x2448d0a 0x194fa78 0x194f9e9 0xecdfbd 0x2aae6 0x2a900 0x56ffbf 0x57021b 0x5704c3 0x580b71 0x5813df 0x581986 0x5815a4 0x34650 0x19a8ec9 0x4ab5c2 0x4ab55a 0x550b76 0x55103f 0x5502fe 0x4d0a30 0x4d0c56 0x4b7384 0x4aaaa9 0x1bbefa9 0x197b1c5 0x18e0022 0x18de90a 0x18dddb4 0x18ddccb 0x1bbd879 0x1bbd93e 0x4a8a9b 0x25cd 0x2535 0x1)
terminate called throwing an exception
谁能帮我?