mrugen munshi
mrugen munshi

Reputation: 3593

Problem creating url

I want to create a url as below http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false

I used the following code to create this

NSURL *jsonURL;
NSString *strurl = [[NSString alloc]initWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];
jsonURL = [NSURL URLWithString:strurl];
[strurl release];

NSLog(@"json Url%@",jsonURL);

NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
if(jsonData == nil){
//NSLog(@"Data NIL .....");
}
else{
    SBJSON *json = [[SBJSON alloc] init];
    NSError *error = nil;
    dic = [json objectWithString:jsonData error:&error]; 
    [json release];
}

But every time I get jsonURL to be nil .

I think the problem is due to "|". Has someone come across same issue? If yes, can you help me out?

Upvotes: 0

Views: 195

Answers (4)

Andrew Pouliot
Andrew Pouliot

Reputation: 5421

As for your URL issue, Georg is right:

NSURL *jsonURL = [NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize%3Atrue%7CBarossa+Valley,SA%7CClare,SA%7CConnawarra,SA%7CMcLaren+Vale,SA&sensor=false"];

Fixed that issue for me.

However, the next bit:

NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

Is deeply troubling. You should never do synchronous data reads on the main thread. initWithContentsOfURL is going to spawn a synchronous NSURLConnection to go fetch that data and might return sometime before sunday, but you never know. (This method is ok for filesystem loads, where things are much more deterministic)

Look into an asynchronous loading API like NSURLConnection from Apple, or better yet ASIHTTPRequest, about which there is ample documentation online.

Happy webservicing!

Upvotes: 2

Georg Fritzsche
Georg Fritzsche

Reputation: 98984

The documentation for URLWithString says:

The string with which to initialize the NSURL object. Must conform to RFC 2396.

... which e.g. mentions:

Other characters are excluded because gateways and other transport agents are known to sometimes modify such characters, or they are used as delimiters.

unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

Data corresponding to excluded characters must be escaped in order to be properly represented within a URI.

Thus escape them properly as slf suggested.

Also, just use a string constant for predefined strings:

NSString *strurl = @"http://....";

Upvotes: 2

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

I think, the root of cause is your string creating method.

NSString *strurl = [[NSString alloc]initWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];

Try with ...

NSString *strurl = [[NSString alloc]initWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];

Upvotes: 1

slf
slf

Reputation: 22767

Try [NSURL URLWithString:[strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]

Upvotes: 3

Related Questions