PDA

View Full Version : http post/get request issue need help from seniors and experianced please




newport123
Nov 3, 2009, 08:39 AM
so i am using this code for a http request to the server to post the gps location of the user along with his username and password. it says connection sucessfull but i do not see anything on the server in the log at all
is there an issue with the code and is there anythig that i can add to the code which can help me weed out the issue






- (void)sendRequest {
NSString *userData = [[NSString alloc] initWithFormat:@"uid=%@&pw=%@&lat=%@&long=%@", usernameField.text, passwordField.text, currentLat, currentLong ];
NSString *post = [[NSString alloc] initWithFormat:@"%@%@", userData, [self getContacts]];
NSLog(post);
NSURL *url = [NSURL URLWithString:@"http://www.test.com/gps"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
NSLog(@"<---Connection successful--->");
}
else {
UIAlertView *noConnectionAlert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Sorry, your request could not be sent. Please check your internet connection and try again." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[noConnectionAlert show];
[noConnectionAlert release];
}
}



robbieduncan
Nov 3, 2009, 08:53 AM
If a valid NSURLConnection object is returned all that means is that you have created a connection object. It most certainly does not mean the connection was successful or that any data was transferred.

You need to implement the delegate protocol and see what the responses are.

bredell
Nov 3, 2009, 09:21 AM
Also, you're doing an HTTP POST but setting the method to GET.

skunkworker
Nov 3, 2009, 11:09 AM
you need to implement the NSURLConnection delegates.
also sending messages like this is asynchronous. try using a queue for knowing exactly where messages came from and went.

newport123
Nov 3, 2009, 11:21 AM
you need to implement the NSURLConnection delegates.
also sending messages like this is asynchronous. try using a queue for knowing exactly where messages came from and went.

this is what is in the delegate
anything else i should put in there


- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response;
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data;
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

robbieduncan
Nov 3, 2009, 11:21 AM
Yes. Actual method bodies that do something!

newport123
Nov 3, 2009, 11:25 AM
Yes. Actual method bodies that do something!

i am a little confused
i am new to the iphone programming

what should i add now for it to work or show me the status of the get/post queries

robbieduncan
Nov 3, 2009, 11:30 AM
Well, for example, this:


- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error;


will get called if hte connection fails but as you have not provided a method body (or more accurately have provided a body that is ; or the empty/null statement) you'll never know). At the very least I'd say you want:


- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog("Connection failed");
}

dejo
Nov 3, 2009, 11:42 AM
i am a little confused
i am new to the iphone programming
Then it's time to step back from the real coding and go (re)learn the basics of Objective-C, OOP, delegation, etc. before you return to this problem. Sorry but you are wasting your time here if you just expect us to give you solutions. As robbieduncan likes to say, "Copying and pasting is not programming."

newport123
Nov 3, 2009, 11:58 AM
Then it's time to step back from the real coding and go (re)learn the basics of Objective-C, OOP, delegation, etc. before you return to this problem. Sorry but you are wasting your time here if you just expect us to give you solutions. As robbieduncan likes to say, "Copying and pasting is not programming."

i agree with u
my intention here is not to get baked code but to know where the issue is so I can learn/work on that. I am running on a tight schedule thats the reason for the post.
There are not a lot of post/get examples online

One last question is there an issue with the - (void)sendRequest

robbieduncan
Nov 3, 2009, 12:09 PM
There are not a lot of post/get examples online
Thats the root point: if you had spent the time understanding how Cocoa Touch works, the language and the concepts you would not need examples. You would read the documents, understand what was going on and write the code. Needing "examples", really code you can copy an paste exactly, for every little thing is not pteogramming. This is not how to learn. Go and spend a couple of weeks reading.

newport123
Nov 3, 2009, 01:53 PM
Thats the root point: if you had spent the time understanding how Cocoa Touch works, the language and the concepts you would not need examples. You would read the documents, understand what was going on and write the code. Needing "examples", really code you can copy an paste exactly, for every little thing is not pteogramming. This is not how to learn. Go and spend a couple of weeks reading.

is there something that u can suggest as in book or online rescource
i just ordered the cocoa book from hilglass and i happen to have steven kochen and cocoa for dummies

thanks a lot for yr time and input

robbieduncan
Nov 3, 2009, 02:22 PM
The documents on the Apple Website are, for me at least, good.

http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html