残念ながら、私は 7 時間にわたって修正 (または理由) を探していましたが、最終的に助けが必要であることを認めました。
私は(Androidから)ios用にアプリケーションを書き直し始めたばかりで、iosでjsonにこだわっています。
私のJSON投稿アクション;
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:@"error_message@"];
}
}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が呼び出されて実行されますが、データベースにデータが入力されていないにもかかわらず、新しい空白行が作成されます。アプリが送信すると主張するものを送信していないように思えます。
正しい方向に微調整していただければ幸いです。