Reputation: 135
I have a string which gives the path, and another which appends the parameters to it. When i put them in a string and display, I'm getting in the correct format. If i try to put the entire string in NSURL, it displays NULL. What is the format to get it?
NSString *booking=urlForBooking.bookHall;
NSLog(@" book %@",booking); // this prints --- http://10.2.0.76:8080/ConferenceHall/BookingHallServlet
NSString *bookingString=[booking stringByAppendingString:[NSString stringWithFormat:@"? employeeId=%@&conferenceHallId=%@&bookingId=%d&purpouse=%@&fromDate=%@&toDate=%@&comments=%@&submit=1",empId,_hallId,_bookingId,_purpose,fromDateStr,toDateStr,_comments]];
NSLog(@"book str %@",bookingString); //this prints --- ?employeeId=3306&conferenceHallId=112&bookingId=0&purpouse=S&fromDate=25/Feb/2013 13:29&toDate=25/Feb/2013 15:29&comments=C&submit=1
NSURL *bookingURL=[NSURL URLWithString:bookingString];
NSLog(@"BOOK %@",bookingURL); //here I'm not getting the url(combined string), it gives null.
Upvotes: 0
Views: 560
Reputation: 69469
That is because the URL your are building contains charters that are not valid in an URL, like spaces and slashes.
You should escape these characters:
NSString *bookingPath =[bookingString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *bookingURL=[NSURL URLWithString:bookingPath];
You might need to replace the slashes in the date because they might not be encoded correctly.
NSString *bookingString=[NSString stringWithFormat:@"%@?employeeId=%@&conferenceHallId=%@&bookingId=%d&purpouse=%@&fromDate=%@&toDate=%@&comments=%@&submit=1",
booking,
empId,
_hallId,
_bookingId,
[_purpose stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[fromDateStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[toDateStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[_comments stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *bookingURL=[NSURL URLWithString:bookingString];
NSLog(@"BOOK %@",bookingURL);
Upvotes: 3
Reputation: 3221
The spaces in _ bookingString_ (before employeeId and in the date) kill your URL.
Upvotes: 0
Reputation: 80603
Your URL string is incorrect in some way, and as such is getting parsed to nil. The documentation for NSURL tells you this can happen:
Return Value An NSURL object initialized with URLString. If the string was malformed, returns nil.
You shouldn't have all those leading spaces after the ?
portion of your URL, and the entire thing needs to be escaped prior to parsing it into a URL.
Upvotes: 1