看板 MacDev 關於我們 聯絡資訊
我照著原po的code去做改寫 目的是希望能改到資料庫中的name欄位中的值 不過試了很久 還是卡在上傳料庫時 NSLog的部分是有讀到東西的 但是資料庫接收到的資料都是空的 想起問各位大大有沒有方法... xcode部分 NSURL *objUrl = [NSURL URLWithString :@"http://xxxxxxx.php"] ; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL : objUrl] ; [theRequest setHTTPMethod : @"POST"]; [theRequest setValue : @"application/json" forHTTPHeaderField : @"Content-Type"]; [theRequest setTimeoutInterval:10]; NSString *stringData = [[NSString alloc] initWithString:@"BOSS"]; NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject:stringData forKey:@"Name"]; NSString *jsonString = [jsonDictionary JSONRepresentation]; NSString *strHttpBody = [NSString stringWithFormat:@"json=%@", jsonString]; NSLog(@"%@",strHttpBody); [theRequest setHTTPBody:[strHttpBody dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; php的部分 <?php $rawJsonData = $_POST['json']; $decodedData = json_decode($rawJsonData); //Connect To Database $hostname='**BLACKEDOUT**.com'; $username='**BLACKEDOUT**'; $password='**BLACKEDOUT**'; $dbname='**BLACKEDOUT**'; $usertable='users'; mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); $query = "UPDATE $usertable SET Name = '$decodedData' ";//do i encode? $result = mysql_query($query); ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.134.69
LouisXIV:我猜 [NSString stringWithFormat:@"jason=%@" <== 07/25 15:31
tentenlee:NSString *jsonString = [jsonDictionary JSONRepresent 07/25 15:41
tentenlee:可以這樣做? 需要匯入甚麼東西嗎? 07/25 15:41
chichi30:謝謝!!可以了!! 07/25 19:40
chichi30:回2F 好像要去匯入SBJSON喔!! 07/25 19:42
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.121.55
tentenlee:........ 我還以為我的推文變成控制碼了.. 08/16 09:02
tentenlee:那你的NSURLConnection的delegate部分勒? 08/16 09:05
痾 可不可以請ten大說簡單一點舉個例子之類的 我還是新手XD 如果是didReceiveResponse那些function我有做了 確定有跑到 didReceiveRespons 內 也有匯入 SBJSON了 BTW 其實我一直在想說 是不是php端的 $rawJsonData = $_POST['json']; <--這行有問題 post內要打其他甚麼之類的? ※ 編輯: shes050117 來自: 140.113.121.55 (08/16 10:17)
tentenlee:我不是大大... PHP部份我都不知道... 08/16 13:32
xevisu:php端要用$_GET 08/16 15:36
shes050117:用$_GET['json']; 這樣? 08/16 15:51
shes050117:還是不行 @@ 08/16 15:54
xevisu:搞錯了 Sorry 08/16 16:52
hchu:Name = $decodedData->{'Name'} 08/17 23:30
Fantasywind:php var_dump($_POST)有東西嗎 08/18 03:20
ilay:你要先弄個什麼東西可以看server return回來的東西 08/20 14:15
ilay:先確認你送出了什麼東西 以及server有沒有收到... 08/20 14:15
ilay:有code http://goo.gl/5YZIH 08/20 14:21
shes050117:h大F大的方法我都是過了 還是不行@@ 可能是我一些東西 08/23 09:30
shes050117:沒用到 最後我是用 $_REQUEST 就OK嘞 08/23 09:30
我最後改了方法 直接用URL傳東西 XCODE 改成這樣 NSMutableString *postString =[NSMutableString stringWithString:@"http://XXX.XXXX.XXX.XXX.php"]; NSString *jsonString =[[NSString alloc] initWithFormat:@"{\"Name\":\"%@\",\"Action\":\"%@\",\"Start\":\"%@\",\"End\":\"%@\"}",postN,postA,postS,postE]; NSLog(jsonString); [postString appendString:[NSString stringWithFormat:@"?data=%@",jsonString]]; [postString setString:[postString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; [request setHTTPMethod:@"POST"]; postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; NSLog(@"post string %@",postString); php 改成這樣 if(isset($_REQUEST['data'])) { $json = $_REQUEST['data']; $data = json_decode($json); } ※ 編輯: shes050117 來自: 140.113.121.55 (08/23 09:49)