I am new to iPhone Development . and my question is on memory management
Here is my code :
- (void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent {
urlAddress = [self encodeStringForURL:urlAddress];
theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
if([postContent length] > 0) {
[theRequest setHTTPMethod:#"POST"];
} else {
[theRequest setHTTPMethod:#"GET"];
}
NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSString *postLength = [NSString stringWithFormat:#"%d", [theBodyData length]];
[theRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody:theBodyData];
[NSThread detachNewThreadSelector:#selector(sendSyncRequest) toTarget:self withObject:nil];
}
I am getting Leaks on these lines :
theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
If anyone can help then help me.
Thanking You.
I am guessing your leak is being created by this line:
urlAddress = [self encodeStringForURL:urlAddress];
I would need to see your code for encodeStringForURL, but my guess if you are passing it back without it autoreleasing. Also, I wouldn't assign the result of that method to your parameter variable, which you will then lose the reference to. You should do something like this instead:
NSString *encodedUrlAddress = [self encodeStringForURL:urlAddress];
Then use encodedUrlAddress when you call URLWithString.
Related
Encountered a problem on posting comment back to a comment box.
post not showing up, there is no error or whatsoever to check.
EDIT: Correct Answer
-(void)postToBoard
{
NSString *post = #"author=Jos&email=mail#domain.com.au&url=&comment=score+99.&submit=Post+Comment&comment_post_ID=1&comment_post_ID=1&comment_parent=0";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSString *URLString = #"http://site.com.au/?p=1";
NSURL *postURL = [NSURL URLWithString:URLString];
[request setURL:postURL];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
//[request setValue:#"wp-comments-post.php" forHTTPHeaderField:#"HTTP/1.1"];
[request setHTTPBody:postData];
//self.receivedData = [NSMutableData data];
self.postConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}
This line looks wrong:
[request setValue:#"wp-comments-post.php" forHTTPHeaderField:#"Content-Type"];
It should be:
[request addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
I'm not sure what you're supposed to do with "wp-comments-post.php", should that perhaps be in the URL instead?
In the past, I created an iPhone app which relies on a sqlite database updated by a php page. When you access the PHP page, it prints out a dump of a sqlite database and the app runs the various queries to update the sqlite database. My previous app worked fine in this manner and would update accordingly. I recently tried to apply the same process to a new app with a much bigger database. I have run into problems.
What I am doing is sending some post variables to a PHP page which recognizes the POST and echoes out the sqlite commands to update the database. The problem is that it takes a few seconds for the page to print the commands. If I have the page print some static text, I get it in my NSLog(#"Response data is going to be ==> %#", result); command. If I have to extract the information from my MySQL database, it takes about 3-5 seconds and then prints (on my normal browser). When I run the app, however, it says that Response data is going to be ==> (null) instead of the commands.
Can anyone shed some light on what might be happening? I know that this works when it doesn't print a lot of stuff and when it is static. The dynamic part makes it return null for some reason.
Help appreciated.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlBase]];
[request setHTTPMethod:#"POST"];
[request setTimeoutInterval:60.0];
NSData *post = [myParameters dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[post length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:post];
[request setHTTPBody:[myParameters dataUsingEncoding:NSUTF8StringEncoding]];
// set headers
NSString *contentType = [NSString stringWithFormat:#"text/plain"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Response data is going to be ==> %#", result);
try to understand this code..it helps u.
NSString *soapMessage=[NSString stringWithFormat:#" YOUR WEB SERVICE CODE",myTxt1.text,myTxt2.text];
NSLog(#"%#",soapMessage);
//-----------create connection
NSURL *url=[NSURL URLWithString:#"http://your local host webservice url"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:#"%d",[soapMessage length]];
//----------sending request
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/AutenticateUser" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
//[activityIndicator startAnimating];
if ( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
hello i am trying to upload my image from myapplication but not able to do so....
i have tried
ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
NSString *nowTimestamp = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970]];
[theRequest setPostValue:kApiKey forKey:#"api_key"];
[theRequest setPostValue:(float)[[NSDate date] timeIntervalSince1970] forKey:#"call_id"];
[theRequest setPostValue:#"1.0" forKey:#"v"];
[theRequest setData:[NSString stringWithString:#"abc"] forKey:#"status"];
[theRequest setPostValue:[NSString stringWithFormat:#"%lld",session1.uid] forKey:#"uid"];
NSLog(#"%lld",session1.uid);
NSString *strSig = [[NSString alloc] init];
strSig = [strSig stringByAppendingString:[NSString stringWithFormat:#"#=%#",#"api_key",kApiKey]];
StrSig = [strSig stringByAppendingString:[NSString stringWithFormat:#"#=%#",#"call_id",nowTimestamp]];
strSig = [strSig stringByAppendingString:[NSString stringWithFormat:#"%#=%#",#"v",#"1.0"]];
strSig = [strSig stringByAppendingString:[NSString stringWithFormat:#"%#=%#",#"uid",[NSString stringWithFormat:#"%lld",session1.uid]]];
strSig = [strSig stringByAppendingString:kApiSecret];
[theRequest setPostValue:[self md5:strSig] forKey:#"sig"];
[theRequest setURL:url];
[theRequest setRequestMethod:#"POST"];
[theRequest setPostFormat:ASIMultipartFormDataPostFormat];
[theRequest startSynchronous];
but it says that signature is incorrect ....
where i am wrong please help me.....
See this thread. If "it" says the signature is incorrect, check the lines where you generate strSig. Do things in smaller chunks so you can see what's going on and NSLog some variables to the console to help. If the code above is a direct copy/paste job, you have a line that uses "StrSig" instead of "strSig," which probably would issue a warning or error, as Obj-C is case sensitive.
i have the following method
-(void)request
{
responseData = [[NSMutableData data] retain];
NSString *post = [NSString stringWithFormat:#"id=%d&a_id=&d",1,1];
NSLog(#"%#",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:kWebURL#"/req/request.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[postLength release];
[postData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
}
My problem is that there is a leak with the nsmutableurlrequest object. I think i should release it somewhere, but when i try to do so, i get an exec_bad_access. Whereever i try to release it i get that error at the end of the connectionDidFinishLoading method.
EDIT: It looks like i can either release the NSURLConnection object OR the NSMutableURLConnection object.
If i try to remove both of them when i should, i get an exec_bad_access
I believe your problem is with your releasing of postLength and postData. You didn't allocate either of these, but created them with convenience methods. You should take those two lines out, and then you should be okay to release your NSMutableURLRequest.
NSString *post = #"&username=adam&password=test";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=adam&pword=test"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSLog(response);
This is my url http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx, now I need to append the username and password dynamically.
How to append querystring in url? Please help me.
You can do it this way:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=%#&pword=%#",name, password]]];