Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newport123

macrumors newbie
Original poster
Oct 22, 2009
23
0
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






Code:
- (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];
	}
}
 
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.
 
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.
 
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


Code:
- (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;
 
Well, for example, this:

Code:
- (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:

Code:
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog("Connection failed");
}
 
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."
 
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
 
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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.