So Ive built an XML parser using the tutorial here: http://www.codeproject.com/Articles/248883/Objective-C-Fundamentals-NSXMLParser
My problem is that the XML file I am calling requires a querystring parameter. An example url would be something like:
http://domain.com/xml.php?id=00idcode00
Without the id= the XML file just returns a single XML element <error>No id specified</error>; with an id specified the XML file returns 50 odd XML elements.
My problem is that no matter what I try the XML file always returns the <error>. How do I get NSURL to use the querystring?
Here is my code:
NSString *someId = #"00idcode00";
NSString *urlString = [NSString stringWithFormat:#"http://domain.com/xml.php?id=%#", someId];
NSURL *url = [NSURL URLWithString:urlString];
parser=[[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
NSLog(#"query: %#",[url query]);
And this NSLogs the following:
Started element error
Found an element named: error with a value of: No id specified.
query: id=00idcode00
The last line where I output the query of url implies that the NSURL has the right querystring, but the response of the XML file suggests that NSURL is not using the querystring.
Can anybody help?
Build the string first, then the URL.
NSString *someId = #"00idcode00";
NSString *urlString = [NSString stringWithFormat:#"http://domain.com/xml.php?Id=%#", someId];
NSURL *url = [NSURL URLWithString:urlString];
And the error states the query param is Id, not id.
Related
I have an NSTextView control that could potentially have links in it. How do I get the full url of the link?
Here is what I have so far
-(BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)aLink atIndex:(NSUInteger)charIndex
{
NSURL *htmlURL = [NSURL fileURLWithPathComponents:[aLink pathComponents]];
}
This gives me a URL that begins with file://localhost ... How do I get rid of that portion of the URL?
NSURL* url = [NSURL URLWithString:#"http://localhost/myweb/index.html"];
NSString* reducedUrl = [NSString stringWithFormat:
#"%#://%#",
url.scheme,
[url.pathComponents objectAtIndex:1]];
I'm calling a JSON URL from the NSURL class. My URL is:
like://Design_Time_Addresses/IServices/RoleService/json/Role/?name=%#",[textField text]];
When I write "doctor" in the textfield I have to get all the details of doctor through that URL. Similarly when I write "engineer" I have to get all the details of engineer. So it means what I type in the textfield, the name in URL should be replaced with the textfield value. But when I write the code like this:
NSURL *jsonUrl =[NSURL URLWithString:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSString *jsonStr =[[NSString alloc] initWithContentsOfURL:jsonUrl];
NSMutableDictionary *jsonDetails = [[NSMutableDictionary alloc]initWithDictionary:[jsonStr JSONValue]];
As shown here I am using the NSURL class to get this URL.
I am getting an error in the NSURL class. How do I pass the text fields value to a URL?
First line should be
NSString *urlString = [NSString stringWithFormat:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSURL URLWithString expects a single string argument, while you are providing two. Since you probably want to construct a single string from the #"http://..." string and [textField text], you should use an NSString method to concatenate them. You can't rely on NSURL to concatenate the string for you.
I have the following code that makes a Google Places API request. The parameters are statically set at the moment. How would I go about making those parameters (types and lat/lon and the Google Key - which I've defined as a constant in the .h file) objects instead?
My problem arises with the NSURL because I can't add format specifiers to it.
thanks for any help.
-(void)ParseXML_of_Google_PlacesAPI
{
NSURL *googlePlacesURL = [NSURL URLWithString:#"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bar&sensor=false&key=MyGoogleAPIKey"];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
NSArray *arr = [xmlDocument.rootElement elementsForName:#"result"];
for(GDataXMLElement *e in arr )
{
[placesOutputArray addObject:e];
}
Good ol' stringWithFormat?
`NSString* urlToCall = [NSString stringWithFormat:#"http:://url.to.webservice/api?param1=%#¶m2=%#", param1, param2]`
This may be helpful for you
float lat=34.0522222,lon=-118.2427778;
NSString *typestr=#"bar";
NSString *key=#"MyGoogleAPIKey";
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=%#&sensor=false&key=%#",lat,lon,typestr,key]];
NSLog(#"url values ==%#",url);
Format specifiers directly to NSURL like this:
NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:#"http:://url.to.webservice/api?param1=%#%param2=%#", param1, param2]];
I'm implementing my drag&drop method. I need that when user drags something on my app window I can get that file URL. NSURL needs to be converted to char. Thats OK. But how to remove file:// from url? My current code:
pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);
its OK, but it gives url with file:// prefix. I tried using
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(#"url: %#", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);
but it says that
Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')
at
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
To go from a file URL to the path as a C string in the appropriate representation for the filesystem, you'd do:
NSURL *fileURL = [NSURL URLFromPasteboard: pboard];
NSString *filePath = [fileURL path];
char *filesystemRepresentation = [filePath filesystemRepresentation];
This avoids assumptions that stripping off the scheme leaves you with just the path, or that the filesystem is definitely happy accepting UTF8-encoded paths.
url = [url stringByReplacingOccurencesOfString:#"file://" withString:#""];
Hope this helps. Cheers!
#user23743's answer is correct. Since iOS 7 though NSURL has its own filestSystemRepresentation method.
In Swift:
if let fileURL = NSURL(fromPasteboard: pboard) {
let representation = fileURL.fileSystemRepresentation
}
if let fileURL = NSURL(from: pboard)?.filePathURL {
}
has been most effective for me.
NSURL * url = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSData * data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
NSLog(#"\nis not nil");
NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
I write this code to download a file from given url... but i get an error on line
NSData * data = [NSData dataWithContentsOfURL:url];
uncaught exception...
so please help me out.
Your first line should be
NSURL * url = [NSURL URLWithString:#"http://192.168.100.161/UploadWhiteB/wh.txt"];
(NSURL is not a string, but can easily be constructed from one.)
I'd expect you to get a compiler warning on your first line--ignoring compiler warnings is bad. The second line fails because dataWithContentsOfURL: expects to be given a pointer to an NSURL object and while you're passing it a pointer that you've typed NSURL*, url is actually pointing to an NSString object.
NSString *file = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSURL *fileURL = [NSURL URLWithString:file];
NSLog(#"qqqqq.....%#",fileURL);
NSData *fileData = [[NSData alloc] initWithContentsOfURL:fileURL];
-[NSString initWithContentsOfURL:] is deprecated. You should be using -[NSString (id)initWithContentsOfURL:encoding:error:]. In either case, the URL paramter is an NSURL instance, not an NSData instance. Of course you get an error trying to initialize a string with the wrong type. You can initialize the string with the URL data using -[NSString initWithData:encoding:], or just initialize the string directly from the URL.