我想在请求中发送两个值:
1)操作字符串
2)用户对象
即使我在参数Operation is not set
"operation": "register"
,我也收到了消息.Success
我是Alamofire的新手。任何人都可以向我解释:
1)如何在请求中发送值?
2)如何发送用户对象?
3)如何处理结果.Failure
和let urlString = URLFactory()
let url = URL(string: urlString.getAppURL())!
print("Log url: \(url)")
let user = User()
user.setEmail(email: email)
let parameters: Parameters = ["operation": "register", "user": user]
Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
print("Log \(response)")
print("Log response.request: \(response.request)")
print("Log response.error: \(response.error)")
print("Log response.data: \(response.data)")
print("Log response.result: \(response.result)")
}
Swift Code:
Log url: http://192.168.0.101/GeolocationNews/NewsDataCrawler/app.php
Log SUCCESS: {
message = "Invalid Parameters";
result = failure;
}
Log response.request: Optional(http://192.168.0.101/GeolocationNews/NewsDataCrawler/app.php)
Log response.error: nil
Log response.data: Optional(51 bytes)
Log response.result: SUCCESS
快速输出:
$login = new Login();
$fun = new FunctionsValidation();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(file_get_contents("php://input"));
if(isset($data->operation)) {
$operation = $data->operation;
if(!empty($operation)) {
if($operation == 'register') {
echo $login->register($data);
}
} else { // if operation is empty
$response["result"] = "failure";
$response["message"] = "Operation is empty";
echo json_encode($response);
}
} else { // if operation is not set
$response["result"] = "failure";
$response["message"] = "Operation is not set";
echo json_encode($response);
}
}
PHP代码:
{
"operation": "register",
"user":
{
"email": "email value"
}
}
更新
我已通过Postman发送测试API:
{"result":"failure","message":"Invalid Email"}
它给了我:class User: NSObject {
private var name: String,
email: String,
password: String,
oldPassword: String,
newPassword: String,
code: String
private var id: Int
override init() {
self.name = ""
self.email = ""
self.password = ""
self.oldPassword = ""
self.newPassword = ""
self.code = ""
self.id = 0
}
// set and get methods ...
}
所以API工作正常!
我尝试通过参数操作发送Alamofire请求并且它有效。所以似乎问题在于将用户对象转换为字典。谁能给我一个如何做到这一点的例子?
用户对象:
os.system()
答案 0 :(得分:0)
我认为问题在于编码。根据您的PHP代码,它接受application/json
作为内容类型,这应该通过Almofire使用JSON编码发送。
请改为尝试:
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
print("Log \(response)")
print("Log response.request: \(response.request)")
print("Log response.error: \(response.error)")
print("Log response.data: \(response.data)")
print("Log response.result: \(response.result)")
}
<强>参考:强> https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#parameter-encoding
答案 1 :(得分:0)
问题在于将用户对象转换为字典。我只是将用户设置为字典而不是使用对象。
let userDictionary: Dictionary = ["email": email, "password": password]
let parameters: Parameters = ["operation": operation, "user": userDictionary]
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
...
}