Reputation: 31
I have looked over numerous threads and am unable to get me code to work, I have a php file that I have used successfully to receive JSON from an android device and insert my values into mySQL
<?php
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["usersJSON"];
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into 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 back to Android Application
echo json_encode($a);
?>
The code I am trying to us in iOS is the 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 to JSON ?
if ([NSJSONSerialization isValidJSONObject:dict])
{
// Serialize the dictionary
json = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
// If no errors, let's view the 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 help would be appreciated. I have successfully been able to get data from the php and save it into my iOS sqlite, but I am having trouble going the other way,
Upvotes: 1
Views: 60
Reputation: 18865
JSON should be encoded in UTF-8, UTF-16 or UTF-32.
So use NSUTF8StringEncoding
instead of NSASCIIStringEncoding
.
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
It wouldn't hurt to set Content-Type to application/json either.
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Upvotes: 2