NSURL - posting & tableview

Discussion in 'iPhone/iPad Programming' started by jjgraz, Apr 7, 2009.

  1. macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    I have been successful posting to a php file using the following code:

    NSString *myRequestString = [[NSString alloc] initWithFormat:mad:"&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:mad:"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.
     
  2. macrumors member

    Joined:
    Mar 20, 2009
    #2
    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:mad:"&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 ];
     
  3. macrumors 68010

    Joined:
    Aug 23, 2008
    #3
    That's because you didn't actually send anything with above codes. Try look into this line: sendSynchronousRequest:returningResponse:error:
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That code just sets up the request: you still have to actually load it using a NSURLConnection.

    I suggest you read the documentation on the URL Loading System.
     
  5. macrumors member

    Joined:
    Mar 20, 2009
    #5
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:mad:"http://monsite/mapage.php"]
    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
    timeoutInterval:60.0];
    [request setHTTPMethod:mad:"POST"];
    [request setHTTPBody:[[NSString stringWithFormat:mad:"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 ?
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. macrumors member

    Joined:
    Mar 20, 2009
    #7
    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) ...
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    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...
     
  9. macrumors member

    Joined:
    Mar 20, 2009
    #9
    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 ...)
     
  10. macrumors newbie

    Joined:
    Apr 17, 2008
    #10
    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.
     
  11. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #11
  12. macrumors newbie

    Joined:
    Apr 17, 2008
    #12
    oh thank you much appreciated
     

Share This Page