views:

67

answers:

4

I'm trying to retrieve data from a POST method, code as follows:

-(void)postXMLFeed:(NSString *)XMLStrPost
{   
    //NSLog (@"XML Feed3: ", XMLStrPost);

    NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.x.xxx/stephen/sync_upload.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[XMLStrPost dataUsingEncoding:NSASCIIStringEncoding]];

    NSURLResponse *response;
    NSError *error;
    response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


    // Update log file.
    NSLog(@"XML feed POSTED to website.");

    //[super postXMLFeed];
}

My XML feed is stored in the variable XMLStrPost. The above code seems to work but I have no way of confirming it.

The script should unparse the string and write it to a database, but this does not seem to be happening. Before the unparse takes place I want to confirm that script is being called.

How can I confirm that sync_upload.php script is called?

A: 

Check if your log file shows an entry from the NSLog call.

Kevin Panko
+2  A: 

Before you even get too deep here, let me seize the opportunity to recommend the All-Seeing Interactive HTTP library, at http://allseeing-i.com/ASIHTTPRequest/. Makes ALL your HTTP interactions vastly simpler.

That said, if you get your NSLog() call in the console, then the synchronous connection you're creating there has returned. You're not testing whether it returned with a failure of some type, though. If I were you, I'd NSLog myself some of the elements of your response object. But again, using ASIHTTP will allow you to ask the request object lots of good questions about its state.

Dan Ray
Thanks very much for this link, I've bookmarked it and its definitely something I'll look in the future, I just don't have the time at the moment to try and figure it out and implement to my project. I'm very close to getting my current code working and can confirm that my PHP script is being called. Thanks again, Stephen
Stephen
A: 

Is it returning anything back to the device when it's done? I'd probably send back something that denotes success or failure from the script for you to read on the iPhone in the response variable.

John Boker
+5  A: 

You're misusing the API. +[NSURLConnection sendSynchronousRequest:returningResponse:error] returns an NSData object, which is the body of the response. You're assigning it into an NSURLResponse object, which means you're not only breaking the type system (you should be getting a warning when compiling), but you're also losing the pointer to the NSURLResponse that the connection gave you.

It's like you're shooting yourself twice in the foot with one bullet... ("Shooting two feet with one bullet"?)

Dave DeLong
Dave,Thanks for the feedback. I changed the line of code your referring to the following:NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];and I can confirm that my PHP script is now being called.
Stephen