从iOS获取JSON以发布到php文件时遇到问题&;插入mySQL
我查看了很多线程,无法让代码正常工作,我有一个php文件,我成功地使用它从android设备接收JSON并将我的值插入mySQL从iOS获取JSON以发布到php文件时遇到问题&;插入mySQL,php,ios,objective-c,json,Php,Ios,Objective C,Json,我查看了很多线程,无法让代码正常工作,我有一个php文件,我成功地使用它从android设备接收JSON并将我的值插入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"]; //Rem
<?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);
?>
任何帮助都将不胜感激。我已经成功地从php获取数据并将其保存到iOS sqlite中,但我在另一方面遇到了困难,应该用UTF-8、UTF-16或UTF-32编码
因此,请使用NSUTF8StringEncoding
而不是NSASCIIStringEncoding
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
将内容类型设置为application/json也没有坏处
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
从iOS代码中得到的错误是什么?iOSHmm中没有错误,NSLog行的输出是什么?
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];