Hi!
As always I have searched the forums and googled my self slightly insane without being able to figure out what I am doing wrong. Therefore I turn to the great minds frequenting this site in hopes to find an answer. I'm building an application that will communicate with a database and in doing so I'm trying to learn how to use JSON to retrieve and post data to the database through the iPhone, using various examples found online. I've managed to retrieve data from the web using JSON and showing it in a tableview, however when I try to POST data nothing works, it seems. Bascially I have a simple php-script that should write out the data it receives to a text file (see below).
<?php
//header('Content-type: application/x-json');
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = var_dump($_POST);
fwrite($fh, $stringData);
$stringData = "=== JSON Decoded ===";
fwrite($fh, $stringData);
$stringData = $_POST["tmp"];
fwrite($fh, json_decode($stringData));
$stringData = "=== JSON Decoded ===";
fwrite($fh, $stringData);
fclose($fh);
?>
The problem is that the script doesn't seem to receive anything. When posting to it, it creates a file that looks like this. So it does create the file and all but there is just nothing in it.
=== JSON Decoded ====== JSON Decoded ===
The code below is my POST method in XCode.
-(IBAction)poststuff:sender{
NSString *stuffToPost = [[NSString alloc] initWithFormat:@"Work, damn you!"];
NSURL *jsonURL = [NSURL URLWithString:@"http://localhost:8888/iWish/json_post.php"];
NSData *postData = [stuffToPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"Stuff I want to POST:%@", stuffToPost);
NSLog(@"Data I want to POST:%@", postData);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:jsonURL];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data = [[NSString alloc] initWithData:serverReply encoding:NSUTF8StringEncoding];
NSLog(@"Raw Data:", data);
}
The console looks like this when triggering the method and writing out the empty text file:
2010-10-04 14:10:16.666 iWish[38743:207] Stuff I want to POST:Work, damn you!
2010-10-04 14:10:16.668 iWish[38743:207] Data I want to POST:<576f726b 2c206461 6d6e2079 6f7521>
2010-10-04 14:10:16.673 iWish[38743:207] serverReply:
It seems to me that the data is there, and formatted and whatnot but for some reason is not getting sent or received. Here's hoping there is just some stupid error somewhere in the code since I've been staring at this for two days now.
I'd appreciate any help. Thanks!