2012-05-08 7 views
0

私のアプリでは、NSURL経由でgoogle方向サービスにjsonリクエストを行う必要があります。NSURLをフォーマットする

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 

誰でも助けてくれますか?

答えて

0

私にはsourceStationまたはdestinationStationのいずれかがnilのようです。デバッグの場合は、NSString *urlString定義の前の行に明示的に設定してみてください。これが動作する場合

NSString *sourceStation = @"SanFrancisco,CA"; 
NSString *destinationStation = @"Dallas,TX"; 

が、それはそれぞれのNSString Sを充填して問題だ...

+0

Heyy ...私はエラーを考え出した 返信用 ありがとう.. 私は配置する必要があります。 NSString * urlEncoded = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; – prasad1250

+0

NSDataのURLメソッドを使用してネットワークリソースを取得しないでください。それは同期的であり、呼び出しスレッドをブロックします。代わりにNSURLSessionを使用してください。 – dgatwood