My aim is to convert NSURL to NSData without any memory leaks... I searched lot and found more than one answer from the website but nothing works for me. Can anyone help me?
Below is the method I tried, but so far nothing works:
NSURL *url = [NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data;
data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSString* contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc]initWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil];
/*do something with data*/
[data release];
[url release];
Note:
When i change my url to http://www.wikipedia.org the code does not have any memory leak... Help me...
Thanks in advance.
Looking at the code samples, you're doing it right - there should be no leak.
The fact that your leak only appears with certain urls make me think that it's happening somewhere else in your code - what are you doing to the data once you have it?
Also, are you testing for leaks on the simulator or the device? The simulator sometimes reports leaks where there aren't any - you should always do a check on the device as well to make sure that it's a real leak and not just the simulator being odd.
Sam
PS To format code, just put 4 spaces in front of each line of code. (or select it and click on the code sample button!)
NSURL *url = [NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data ; data= [NSData dataWithContentsOfURL:url];
NSError error; NSString contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc]initWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil];
/do something with data/
[data release];
[url release];
Related
NSJSONSerialization returns null when i try to implement a particular api from the server, but other api's from the server works fine , when i tried the api in web it returns data of json type , and i have checked it in json viewer also , and it worked fine , NSData also returns some data, can Anyone help me get through it ?
NSString *urlString = #"url";
NSURL *url = [NSURL URLWithString:urlString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url];
_dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(error != nil){
NSLog(#"JSON Error: %#", error);
return;
}
I have tried the above written code.
---Edited-----
Since it was a issue from server side as server was sending an Invalid Character along with response, In json validator it was passing but not in a actual Program , Hence closing the question.
The Problem is of Escape Sequences in your URL and you are not escaping them. You have to escape those sequences properly.
NSString *urlString = #"url";
NSString *encodedString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
Take a Look at Documentation.
Check also the Escape Sequences.
Try this :
NSString *urlString = #"url";
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Make a NSURLConnection and request the JSON with NSURLRequest because I don't think the 'json' is an actual file that has content so you could use dataWithContentsOfURL:
i getting data from my site:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
contents have Cocoa error 256. where i wrong?
The issue is in Hebrew characters, you should html-escape them, also try request with English characters instead, to see if it works
- (void)yourMethod
{
NSString *name = #"שימרגוליס";
name = AFURLEncodedStringFromStringWithEncoding(name, NSUTF8StringEncoding);
NSString *website = [NSString stringWithFormat:#"http://www.ba-cafe.com/fbconnect.php?email=%#&name=%#&pass=SwHyK17!",#"email#mail.com",name];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
}
Where AFURLEncodedStringFromStringWithEncodingis a function from AFNetworking framework
Check the Console log for NSLog(#"%#", website);
You will see something like this:
http://www.mysite.com/fbconnect.php?email=thetrimmedname&name=emailaddress&pass=***
So do this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", [jsonObject objectForKey:#"email"], nameTrimmmed ];
instead of this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
This is because of the dot in the email address.
Look right here:
Error while trying to access Google translate with NSURL
I have the following php file:
<?php
$username="root";
$database="testdb";
mysql_connect(localhost,$username);
$user=$_GET["user"];
$password=$_GET["password"];
$query="SELECT documento FROM person WHERE user='".$user." and password ='".$password."'";
$result=mysql_query($query) or die (mysql_error("error "));
$num=mysql_numrows($result);
mysql_close();
$rows=array();
while($r=mysql_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>
to retrieve the information achieved by the following functions but the example did not have the php input conditions and parameters was select * from person here the functions
based in http://www.youtube.com/watch?feature=endscreen&v=IQcLngIDf9k&NR=1
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
now as the sql statement has input parameters is not as code and tried something like this A Simple PHP/MySQL Web Service for iOS but I can not accommodate what I need.
Pass the parameters in the string. Here is an example
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#?password=%#", user, password];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
As it is password and user it normally is better to use POST instead of GET because then it is not part of the URL which is visible. With POST you can hide it if you have a https line. But I think this is not your main concern right now. The above should work.
I made some change in Hollfeder's code and it worked perfectly :)
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#&password=%#", user, password];
NSString *urlstr_encoded = [urlstr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlstr_encoded];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
To all
I am trying to parser the xml in my project But I have the code for xml parsing for locally I want the server base xml parsing how can i pass that
This is my xml parsing link i want to use:
http://www.google.com/ig/api?weather=,,,50500000,30500000
This my xml parsing for calling local file how can I call my webserver xml link for parsing.I need different-differnet type of calling xml link for parsing:
- (void)viewDidLoad
{
[super viewDidLoad];
_forecastInfo=nil;
currentcond=nil;
forecastcond=nil;
// Pass the NSData to the parser whether its from local file or its coming from web.... NSString *fileName=[[NSBundle mainBundle] pathForResource:#"weather" ofType:#"xml"];
NSData *data=[NSData dataWithContentsOfFile:fileName];
ForecastInfoParser *parser=[[ForecastInfoParser alloc]init];
parser.delegate=self;
[parser parseData:data];
}
NSURL *url = [[NSURL alloc] initWithString:#"hhttp://www.google.com/ig/api?weather=,,,50500000,30500000"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2 -- parsing
parser = [[MyParser alloc] init];
[parser parseXML:data];
NSString *URL=#"http://www.google.com";
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
NSString * theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
[resp release];
[err release];
NSLog(#"response: %#", theString);
I am trying to download an HTML file from the internet and store it in my app for later (offline) viewing. I use the following method to download the file but my NSLogs report that no data has been downloaded. Whats the problem here? (assume an active internet connection, assume a pre-defined path to local documents directory)
urlAddress = #"http://subdomain.somesite.com/profile.html";
// Create and escape the URL for the fetch
NSString *URLString = [NSString stringWithFormat:#"%#%#", #"ttp://subdomain.somesite.com/profile.html"];
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
// Do the fetch - blocks!
NSData *theData = [NSData dataWithContentsOfURL:URL];
if(theData == nil) {
NSLog(#"NO DATA DOWNLOADED");
}
// Do the write
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:#"Profile/profile.html"];
[theData writeToFile:filePath atomically:YES];
NSLog(#"WRITING profile.html to path %#!", filePath);
Because you shouldn't do the stringByAddingPercentEscapesUsingEncoding: stuff, which turn the URL into an invalid one http%3a%2f%2f....
Directly use
NSData *theData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://subdomain.somesite.com/profile.html"]];