1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

http post/get request issue need help from seniors and experianced please

Discussion in 'iPhone/iPad Programming' started by newport123, Nov 3, 2009.

  1. macrumors newbie

    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]];
    	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];
  2. Moderator


    Staff Member

    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.
  3. macrumors regular

    Also, you're doing an HTTP POST but setting the method to GET.
  4. macrumors regular

    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.
  5. macrumors newbie

    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;
  6. Moderator


    Staff Member

    Yes. Actual method bodies that do something!
  7. macrumors newbie

    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
  8. Moderator


    Staff Member

    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");
  9. Moderator


    Staff Member

    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."
  10. macrumors newbie

    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
  11. Moderator


    Staff Member

    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.
  12. macrumors newbie

    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
  13. Moderator


    Staff Member

Share This Page