【发布时间】:2018-02-22 11:52:30
【问题描述】:
我正在做一个项目,我需要将一些数据发送到远程数据库。所以我正在使用 Swift 3.1 开发一个 iOS 应用程序,当我尝试将数据发送到它说的数据库时,
The data couldn’t be read because it isn’t in the correct format.
还有一个错误;
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
这是我的快速代码:
let urlOfSMARTCF = URL(string: "http://192.168.1.99/insertData.php")
let request = NSMutableURLRequest(url: urlOfSMARTCF! as URL)
request.httpMethod="POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
for contact in contactsCaptuure
{
let userMobileNumber = DBManager.shared.retriveRegisteredNumberOfMobile()
let postParameters = "{\"usermobilenum\":\(String(describing: userMobileNumber!)),\"contactnum\":\(contact.phoneNumber!)}";
request.httpBody = postParameters.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest)
{
data, response, error in
if error != nil
{
print("error is \(String(describing: error))")
return;
}
do
{
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = myJSON
{
var msg : String!
msg = parseJSON["message"] as! String?
print(msg)
}
}
catch
{
print(error.localizedDescription)
print(error)
}
}
print("Done")
task.resume()
}
这是我在远程数据库中的 PHP:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
require 'connectDB.php';
$userPhone = $_POST["usermobilenum"];
$contactNum = $_POST["contactnum"];
$query = "SELECT * FROM user WHERE UserMobNum='".$userPhone."'"; // Usermobile is registered.SIP exists.
if($results= mysqli_query($connect,$query))
{
if(mysqli_num_rows($results)>0)
{
$i=0;
while($rows=mysqli_fetch_assoc($results))
{
$sip[$i] = $rows["SIP"];
$i++;
}
$queryToAddData = "INSERT INTO user (UserMobNum,SIP,Phone) VALUES ('".$userPhone."','".$sip[0]."','".$contactNum."')";
if(mysqli_query($connect,$queryToAddData))
{
//Return success message to the app
echo "Success"
}
else
{
die(mysqli_error($connect));
}
}
else
{
$availableSIP=false;
while($availableSIP==false) // Assign a random value until it's being a valid one.
{
$sip[0]=rand(1,9999);
$queryToCheck = "SELECT * FROM user WHERE SIP='".$sip[0]."'";
if($results= mysqli_query($connect,$queryToCheck))
{
if(mysqli_num_rows($results)==0)
{
$availableSIP=true;
}
}
}
$queryToAddData = "INSERT INTO user (UserMobNum,SIP,Phone) VALUES ('".$userPhone."','".$sip[0]."','".$contactNum."')";
if(mysqli_query($connect,$queryToAddData))
{
//Return success message to the app
echo "Success"
}
else
{
die(mysqli_error($connect));
}
}
}
else
{
echo "First Level Failure!";
die(mysqli_error($connect));
}
mysqli_close($connect);
}
else
{
echo "Failed in POST Method"
}
?>
我做了什么
查看了所有堆栈溢出和其他站点建议,但没有运气。我什至使用 json 验证器检查了我的 json 字符串,它通过了。这就是我的 json 字符串的样子。
{"usermobilenum":1234567890,"contactnum":9345}
但是,经过一番搜索,我发现这是因为远程数据库 PHP 发送了此错误消息。所以我检查了 PHP 中的每一个变量,但没有发现任何问题。这也不是 PHP 的问题,因为当我通过我的 android 应用程序连接时,我使用的是那些确切的 php 文件。这很好用。但在 iOS 中,它会产生该错误。有人可以帮帮我吗?
更新
这是 insertdataTest.php 文件:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$userPhone = $_POST["usermobilenum"];
echo $userPhone;
mysqli_close($connect);
}
else
{
echo json_encode("Failed in POST Method");
}
?>
【问题讨论】:
-
@ficuscr 试过了。运气不好
-
其他代码中有什么?数据是否存储在数据库中?你还给json吗?你确定你没有在某处打印一些字符串吗?
-
@GabrieleCarbonai 我使用了一些打印功能来检查 swift 代码中的值。数据存储在 SQLite 数据库中,并通过 FMDB 库检索并转换为字符串。
-
尝试通过浏览器打开创建json的文件,如果格式不正确会在浏览器中写入,然后你可以处理它