Having problems getting my JSON from iOS to post to my php file & insert into mySQL -
i have looked on numerous threads , unable me code work, have php file have used receive json android device , insert values mysql
<?php include_once './db_functions.php'; //create object db_functions clas $db = new db_functions(); //get json posted android application $json = $_post["usersjson"]; //remove slashes if (get_magic_quotes_gpc()){ $json = stripslashes($json); } //decode json array $data = json_decode($json); //util arrays create response json $a=array(); $b=array(); //loop through array , insert data read json mysql db for($i=0; $i<count($data) ; $i++) { //store user mysql db $res = $db->storechecklist($data[$i]->customername, $data[$i]->address, $data[$i]->date,$data[$i]->device,$data[$i]->operator,$data[$i]->installer,$data[$i]->complete,$data[$i]->check1,$data[$i]->check2,$data[$i]->check3,$data[$i]->check4,$data[$i]->column_check5,$data[$i]->check6,$data[$i]->check7,$data[$i]->check8,$data[$i]->check9,$data[$i]->check10,$data[$i]->check11,$data[$i]->check12,$data[$i]->check13,$data[$i]->lite_id,$data[$i]->user,$data[$i]->syncid,$data[$i]->photo1,$data[$i]->photo2,$data[$i]->photo3,$data[$i]->photo4,$data[$i]->photo1b,$data[$i]->photo2b,$data[$i]->photo3b,$data[$i]->photo4b,$data[$i]->completed_by); //based on inserttion, create json response if($res){ //$b["id"] = $data[$i]->column_id; $b["status"] = 'yes'; array_push($a,$b); }else{ // $b["id"] = $data[$i]->column_id; $b["status"] = 'no'; array_push($a,$b); } } //post json response android application echo json_encode($a); ?>
the code trying in ios following:
nsdictionary *dict = @{@"customername" : self.txtcustomername.text, @"address" : self.txtlocation.text, @"date" : self.txtdate.text, @"device" : self.txtdevice.text, @"operator" : self.txtoperator.text, @"installer" : self.txtinstaller.text, @"complete" : self.txtdatecomplete.text, @"check1" : self.txtcheck1.text, @"check2" : self.txtcheck2.text, @"check3" : self.txtcheck3.text, @"check4" : self.txtcheck4.text, @"check5" : self.txtcheck5.text, @"check6" : self.txtcheck6.text, @"check7" : self.txtcheck7.text, @"check8" : self.txtcheck8.text, @"check9" : self.txtcheck9.text, @"check10" : self.txtcheck10.text, @"check11" : self.txtcheck11.text, @"check12" : self.txtcheck12.text, @"check13" : self.txtcheck13.text, @"lite_id" : self.txtuniqueid.text, @"user" : self.txtuser.text, @"syncid" : self.txtsyncid.text, @"photo1" : self.txtphoto1.text, @"photo2" : self.txtphoto2.text, @"photo3" : self.txtphoto3.text, @"photo4" : self.txtphoto4.text, @"photo1b" : @"blank", @"photo2b" : @"blank", @"photo3b" : @"blank", @"photo4b" : @"blank" ,@"completed_by" : self.txtemployee.text // ,@"array" : arrayofobjects }; nserror *error = nil; nsdata *json; // dictionary convertable json ? if ([nsjsonserialization isvalidjsonobject:dict]) { // serialize dictionary json = [nsjsonserialization datawithjsonobject:dict options:0 error:null]; // if no errors, let's view json if (json != nil && error == nil) { nsstring *jsonstring = [[nsstring alloc] initwithdata:json encoding:nsutf8stringencoding]; nsstring *querystring = [nsstring stringwithformat:@"http://myurlhere.com/insertchecklist.php=%@", jsonstring]; nsmutableurlrequest *therequest=[nsmutableurlrequest requestwithurl:[nsurl urlwithstring: querystring] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; [therequest sethttpmethod:@"post"]; nsstring *post = [nsstring stringwithformat:@"usersjson=%@", jsonstring]; nsdata *postdata = [post datausingencoding:nsasciistringencoding]; [therequest sethttpbody:postdata]; nsurlconnection *con = [[nsurlconnection alloc] initwithrequest:therequest delegate:self]; if (con) { // _receiveddata=[nsmutabledata data]; nslog(@"it connected"); } else { //something bad happened nslog(@"something bad happend"); } nslog(@"usersjson: %@", jsonstring); // [jsonstring release]; }
any appreciated. have been able data php , save ios sqlite, having trouble going other way,
json should encoded in utf-8, utf-16 or utf-32.
so use nsutf8stringencoding
instead of nsasciistringencoding
.
nsdata *postdata = [post datausingencoding:nsutf8stringencoding];
it wouldn't hurt set content-type application/json either.
[therequest setvalue:@"application/json" forhttpheaderfield:@"content-type"];
Comments
Post a Comment