iOS JSON POST not accepted by php -


unfortunately have been searching fix (or reason) 7 hours , have conceded need help.

i have started rewriting application ios (from android) , stuck json in ios.

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:@"error_message@"];        }     }else {        if (error) nslog(@"error: %@", error);     } } 

and php;

<?php    include_once("../php/sign_up/connect_db.php"); // array json response $response = array();  // check 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 db $db = new db_connect();     $result = mysql_query("select *from user_signup (email = '".$email."')" ) or die(mysql_error());  $existingemails = mysql_num_rows($result);   if ($existingemails > 0) {   $response["success"] = 0;  $response["message"] = "email address exists";   //echoing json response  echo json_encode($response);  }  else {    // mysql inserting new row $result = mysql_query("insert 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) {     // inserted database     $response["success"] = 1;     $response["message"] = "thank signing up!";      // echoing json response     echo json_encode($response); } else {     // failed 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 missing    $response["success"] = 0;    $response["message"] = "please check have completed required fields";         // echoing json response        echo json_encode($response);       }      ?> 

and json returned;

    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     have completed required fields"}  2013-10-07 00:03:59.646 ***[2517:c07] {  message = "please check have completed required fields";  success = 0;  } 

now android app posts json array in same format , accepted php. cannot life of me work out wrong.

it appear removing check "if empty" php php called , run yet no data entered database yet new blank row created. me sounds app not sending claims send.

any nudges in correct direction appreciated.

json strings enclosed in double quotes, not single quotes, , dictionary key/value pairs must separated comma. see json.org more information.

note there nsjsonserialization class, can create json data nsdictionary.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -