2010-10-04 66 views
5

與往常一樣,我搜索論壇,並搜索我的自己有點瘋狂,但無法弄清楚我做錯了什麼。因此,我轉向期待這個網站的偉大思想,希望找到答案。 我正在構建一個將與數據庫進行通信的應用程序,並試圖通過使用在線發現的各種示例來學習如何使用JSON檢索數據並將其發佈到數據庫。我設法使用JSON從Web上檢索數據並在TableView中顯示它,但是當我嘗試POST數據時沒有任何效果。基本上,我有一個簡單的php腳本,它應該將它接收到的數據寫入文本文件(見下文)。寫出JSON數據到一個簡單的文本文件

<?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); 
?> 

問題是腳本似乎沒有收到任何東西。發佈到它時,它會創建一個看起來像這樣的文件。所以它確實創建了文件和所有文件,但沒有任何內容。

=== JSON Decoded ====== JSON Decoded === 

下面的代碼是我在XCode中的POST方法。

-(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); 
} 

控制檯看起來是這樣的觸發時的方法和寫出來的空文本文件:

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: 

在我看來,數據是存在的,並格式化和諸如此類的東西,但由於某些原因沒有得到發送或接收。這裏希望在代碼中有一些愚蠢的錯誤,因爲我現在已經注意了兩天了。

我很感激任何幫助。謝謝!

回答

3

With fwrite您只能寫入字符串,並且var_dump不會返回字符串。 而...,json_decode不起作用,因爲您的發佈請求無效JSON。

所以,我認爲這會爲你工作:

$myFile = "testFile.txt"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
$stringData = json_encode($_POST); 
fwrite($fh, $stringData); 
fclose($fh); 
+0

感謝您的答覆。我嘗試了代碼,但它只在文本文件中寫出[]。你提到我的帖子請求不是有效的JSON。這是爲什麼?我錯過了什麼? – Glitch 2010-10-04 13:11:38

+0

不,如果你想使用json_decode(在你的例子中),POST請求也必須以JSON格式(..so遠是我可以讀取XCode,它不是)。 如果將$ _POST var更改爲$ _GET var,並調用http:// localhost:8888/iWish/json_post.php?var = test&var2 = example?會發生什麼?你在你的測試文件中看到有效的JSON字符串嗎? – 2010-10-04 13:23:17

+0

如果我將$ _POST更改爲$ _GET並調用localhost:8888/iWish/json_post.php?var = test&var2 = example,則寫出的測試文件如下所示:{「var」:「test」,「var2」: 「例」}。 – Glitch 2010-10-05 09:34:38