PDA

View Full Version : NSURL - posting & tableview




jjgraz
Apr 7, 2009, 11:17 AM
I have been successful posting to a php file using the following code:

NSString *myRequestString = [[NSString alloc] initWithFormat:@"&first=%@", number];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://awebsite.com/file.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];

I have also been successful loading and parsing a TableView using the following:

NSURL *url = [[NSURL alloc] initWithString:@"http://awebsite.com/file.php"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

I can't figure out how to combine the two efforts though. I would like to have my TableView updated with the contents of URL, but only after the post is complete. This way the variable sent can inform the PHP file the correct data to send back.

If you have any ideas, please advise. I appreciate it.



bboyjayz
Apr 29, 2009, 11:01 AM
I've a problem, i tried this and it doesn't work... i juste try to send my informations about my profil to a php page, but nothing appear on my php page.... do you see some errors?


NSString *myRequestString = [[NSString alloc] initWithFormat:@"&imei=111111&nickname=Jayz&score=999"];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://monsite.com/mapage.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];

CocoaPuffs
Apr 29, 2009, 11:08 AM
I've a problem, i tried this and it doesn't work... i juste try to send my informations about my profil to a php page, but nothing appear on my php page.... do you see some errors?


NSString *myRequestString = [[NSString alloc] initWithFormat:@"&imei=111111&nickname=Jayz&score=999"];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://monsite.com/mapage.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
That's because you didn't actually send anything with above codes. Try look into this line: sendSynchronousRequest:returningResponse:error:

robbieduncan
Apr 29, 2009, 11:10 AM
That code just sets up the request: you still have to actually load it using a NSURLConnection.

I suggest you read the documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html) on the URL Loading System.

bboyjayz
Apr 29, 2009, 11:31 AM
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://monsite/mapage.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSString stringWithFormat:@"score=222"] dataUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection) {
NSMutableData* receivedData = [[NSMutableData data] retain];
//[connection connection:connection willSendRequest:request redirectResponse:nil];
}



This code should work doesn't it ? 'cause it doesn't .... do youi know why ?

robbieduncan
Apr 30, 2009, 02:34 AM
Erm, on it's own that won't end up with any data in receivedData. You need to read the rest of the document where you got that from, especially the connection:didReceiveData: delegate method.

bboyjayz
Apr 30, 2009, 06:34 AM
Even if the only one things i want to do is to send some informations to my php page ? beacause it's the only one things i want to do: when a player finish game, his score is send to my php page (which make the TOP10) ...

robbieduncan
Apr 30, 2009, 06:43 AM
Even if the only one things i want to do is to send some informations to my php page ? beacause it's the only one things i want to do: when a player finish game, his score is send to my php page (which make the TOP10) ...

Well, if I was you I'd implement all the delegate methods to allow yourself to check the responses from the server: they might actually tell you what's going on...

bboyjayz
Apr 30, 2009, 11:12 AM
and can't we just create a method who send information to my php page, and that's all ? (create this method on a UIViewController, or delegate ...)

firecat
May 13, 2009, 04:01 AM
uh bboyjayz you took over the thread. :) I would really like to know what the answer to the original question was : how to post variables and receive a response in one fell swoop.

jnic
May 13, 2009, 04:22 AM
I would really like to know what the answer to the original question was : how to post variables and receive a response in one fell swoop.

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

"data" contains the data response to your request.

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

firecat
May 15, 2009, 01:57 AM
oh thank you much appreciated