php - 如何使用 PHP 和 Swift 5 解析 JSON
问题描述
我正在通过 PHP 在我的应用程序中解析 JSON,我的消息中心工作正常,但现在它停止工作了。我正在使用 Swift 5、PHP 和 MySQL。
当我尝试在应用程序中查看页面时,我收到一条 JSON 错误消息,
'无法读取数据,因为它的格式不正确'。
使用相同代码的其他页面没有此问题。
我尝试了不同的属性,JSONSerialization
但没有任何效果。
Swift 5 代码从 php 加载消息
func loadMessages() {
let username = user!["username"] as! String
let url = URL(string: "https://www.xxxx.com/messagecenter.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(username)"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
if error == nil {
do {
let json : NSDictionary? = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
self.tableView.reloadData()
guard let parseJSON = json else {
print("Error while parsing")
return
}
guard let messages = parseJSON["messages"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}
self.hhmessages = messages
for i in 0 ..< self.hhmessages.count {
let path = self.hhmessages[i]["ava"] as? String
if !path!.isEmpty {
let url = URL(string: path!)!
let imageData = try? Data(contentsOf: url)
let image = UIImage(data: imageData!)!
self.avas.append(image) // append found image to [images] var
} else {
let image = UIImage()
self.avas.append(image)
}
}
self.tableView.reloadData()
} catch {
Helper().showAlert(title: "JSON Error", message: error.localizedDescription, in: self)
return
}
} else {
}
})
}.resume()
}
PHP 代码
$access->connect();
$returnArray = array();
if (!empty($_REQUEST["uuid"]) && empty($_REQUEST["id"])) {
// STEP 2.1 Get uuid of post and path to post picture passed to this php file via swift POST
$uuid = htmlentities($_REQUEST["uuid"]);
//$path = htmlentities($_REQUEST["path"]);
// STEP 2.2 Delete post according to uuid
$result = $access->deleteMessage($uuid);
if (!empty($result)) {
$returnArray["message"] = "Successfully deleted";
$returnArray["result"] = $result;
} else {
$returnArray["message"] = "Could not delete post";
}
}
else {
// STEP 2.1 Pass POST / GET via html encrypt and assign passed id of user to $id var
$username = htmlentities($_REQUEST["username"]);
// STEP 2.2 Select posts + user related to $id
$messages = $access->messageCenter($username);
// STEP 2.3 If posts are found, append them to $returnArray
if ($messages) {
$returnArray['messages'] = $messages;
}
}
// STEP 3. Close connection
$access->disconnect();
// STEP 4. Feedback information
echo json_encode($returnArray);
?>
该代码应为用户显示收件箱。
解决方案
使用以下步骤解决了该问题:
- 打印 print(String(data: data!, encoding: .utf8))
- 错误消息显示我的文件被 GoDaddy 阻止
- 将我的页面添加到 Godaddy 安全门户的白名单中
- 有效。
推荐阅读
- amazon-web-services - AWS SES - 使用不同的账户发送电子邮件
- google-sheets - 自动对 Google 表格中的所有列或行进行编号/计数
- javascript - 面向对象 JavaScript 中的继承
- css - CSS 代码未应用于我的烧瓶应用程序
- mongodb - Mongoose find 返回一个空数组,但是当我为模型设置变量时返回值
- java - 在 Java 中的大二进制文件上匹配十六进制正则表达式 (\x)
- async-await - TypeError:无法读取未定义的属性“类型”(redux 工具包)
- karate - 如何使用空手道框架验证来自 api 响应的时间戳是否是最新的
- plsql - PL/SQL:我收到错误“遇到符号“OPEN”时预期以下之一:. (* @ % & - + /"
- c# - 使用/到 Linq 运行/转换原始 SQL 层次结构?