0

不幸的是,我已经搜索了 7 个小时的修复(或原因),最终承认我需要一些帮助。

我刚刚开始为 ios 重写我的应用程序(来自 android),我在 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.ini 完美接受。我终其一生都无法找出问题所在。

它会通过从 php 中删除检查“如果为空”来显示,调用并运行 php,但没有数据输入到数据库中,但会创建一个新的空白行。对我来说,这听起来像是该应用程序没有发送它声称要发送的内容。

任何朝着正确方向的轻推将不胜感激。

4

1 回答 1

0

JSON 字符串用双引号括起来,而不是单引号,并且字典键/值对必须用逗号分隔。有关更多信息,请参阅 json.org。

请注意,有一个 NSJSONSerialization 类,它可以从 NSDictionary 创建 JSON 数据。

于 2013-10-06T23:56:10.727 回答