2013-10-06 69 views
0

不幸的是我一直在尋找修復(或原因)7小時,現在接受並終於承認我需要一些幫助。的iOS JSON POST PHP不

我剛開始重寫我的iOS應用程序(從Android)和我被困在IOS JSON。

My JSON post action;

 NSString *post = [[NSString alloc] initWithFormat:@"{'firstname':'%@' 'surname':'%@'  'yob':'%@' 'gender':'%@' 'hometown':'%@' 'phone':'%@' 'email':'%@' 'deviceid':'%@' 'regId':'%@' 'phonetype':'%@'}",[_FirstName text],[_Surname text],[_YOB text],[_Gender text],[_HomeTown text], [_PhoneNO text],[_Email text],[_deviceid text],[_regID text],[_phonetype text]]; 

    NSLog(@"PostData: %@",post); 

    NSURL *url=[NSURL URLWithString:@"***"]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

    [request setURL:url]; 

    [request setHTTPMethod:@"POST"]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

    [request setValue:@"appplication/www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    [request setHTTPBody:postData]; 

    NSError *error = [[NSError alloc] init]; 

    NSHTTPURLResponse *response = nil; 

    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    NSLog(@"Response code:%d", [response statusCode]); 

    if ([response statusCode] >=200 && [response statusCode] <300) 

    { 

     NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 

     NSLog(@"Respinse ==> %@", responseData); 

     SBJsonParser *jsonParser = [SBJsonParser new]; 

     NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil]; 

     NSLog(@"%@",jsonData); 

     NSInteger sucess = [(NSNumber *) [jsonData objectForKey:@"sucess"] integerValue]; 

     NSLog(@"%d",sucess); 

     if (sucess==1) 

     { 

      NSLog(@"Signed up"); 

     }else { 

      NSString *error_msg = (NSString *) [jsonData objectForKey:@"[email protected]"]; 
     } 
    }else { 
     if (error) NSLog(@"Error: %@", error); 
    } 
} 

和我的php;

<?php 


include_once("../php/sign_up/connect_db.php"); 
// array for JSON response 
$response = array(); 

// check for required fields/ 
if (!empty($_POST['firstname']) && !empty($_POST['surname']) && !empty($_POST['email']) &&  !empty($_POST['phone']) && !empty($_POST['hometown']) && !empty($_POST['deviceid']) && !empty($_POST['yob']) && !empty($_POST['regId']) && !empty($_POST['gender'])) { 

$firstname = $_POST['firstname']; 
$surname = $_POST['surname']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$hometown = $_POST['hometown']; 
$deviceid = $_POST['deviceid']; 
$phonetype = $_POST['phonetype']; 
$yob = $_POST['yob']; 
$regId = $_POST['regId']; 
$gender = $_POST['gender']; 

// include db connect class 
//require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 

    $result = mysql_query("SELECT *FROM User_signup WHERE (email = '".$email."')") or die(mysql_error()); 
$existingemails = mysql_num_rows($result); 

if ($existingemails > 0) { 

$response["success"] = 0; 
$response["message"] = "Email address already exists"; 

//echoing JSON response 
echo json_encode($response); 

} 
else { 



// mysql inserting a new row 
$result = mysql_query("INSERT INTO User_signup(firstname, surname, email, phone, hometown, id, type, Yob, regid, gender) VALUES('$firstname', '$surname', '$email', '$phone', '$hometown', '$deviceid', '$phonetype', '$yob', '$regId', '$gender')"); 

// check if row inserted or not 
if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "Thank you for signing up!"; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = "An Error has occurred, please try again later."; 

    // echoing JSON response 
    echo json_encode($response); 
} 
} 

    }else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Please check you have completed all required fields"; 

     // echoing JSON response 
     echo json_encode($response); 
     } 

    ?> 

這就是返回的JSON;

2013-10-07 00:03:59.465 ***[2517:c07] PostData: {'firstname':'ouyb' 'surname':'ouy' 'yob':'vbouv' 'gender':'GENDERouvy' 'hometown':'ouvy' 'phone':'ou' 'email':'vy' 'deviceid':'ilbuyv' 'regId':'obyouovy' 'phonetype':'uby'} 

2013-10-07 00:03:59.645 ***[2517:c07] Response code:200 

2013-10-07 00:03:59.645 ***[2517:c07] Respinse ==> {"success":0,"message":"Please check you  have completed all required fields"} 

2013-10-07 00:03:59.646 ***[2517:c07] { 

message = "Please check you have completed all required fields"; 

success = 0; 

} 

現在我的android應用程序發佈json數組的格式相同,並被php完全接受。我不能爲我的生活找出什麼是錯的。

這將通過從PHP PHP的取消勾選「若爲空」的出現被調用,運行但沒有數據被輸入到又一個新的空白行創建的數據庫。對我來說,這聽起來像應用程序沒有發送它聲稱發送的內容。

在正確的方向上的任何輕推,將不勝感激。

回答

0

JSON字符串包括在雙引號,不單引號,和字典的鍵/值對必須用逗號分開。有關更多信息,請參閱json.org。

注意,有一個NSJSONSerialization類,它可以從一個NSDictionary中創建JSON數據。

+0

你好,謝謝你的迅速回復。我似乎無法包含雙引號。至少它們不會出現在發送的params日誌中。至於逗號是代替冒號? – James

+0

@mark:不,JSON必須看起來像「{」firstname「:」ouyb「,」surname「:」ouy「,...}。但是看看NSJSONSerialization,可以讓事情變得更簡單。關於stackoverflow的NSJSONSerialization有很多Q&A。 –

+0

謝謝。我試過把@「{」firstname「:」%@「,...},但是我得到一個錯誤」Lexical or Preprocessor issue Expected':''期待我將第二個名字替換爲a:any想法?我將開始尋找NSJSONSerialization謝謝你的提示。 – James