出现问题时间:2017/09/14 12:49
问题描述:
Alamofire 错误提示1:
“responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))”
错误原因:返回非正确类型
"Content-Type" = "text/html"; 导致json序列化失败:jsonSerializationFailed
正确的类型为 “Content-Type”:“application/json”
导致content-type 不是服务端预定json数据类型 可能是因为服务端出现500错误 继续调试如下
错误提示2:
Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500)
错误原因:返回服务器内部错误500 原因是数据库操作错误
解决方案:
添加手工验证 调试打印返回信息
url = "domain/”
Alamofire.request(url, method: .post, parameters: ["contacts": convertArrayToString(arr: resultArray)])
// 添加手工验证返回Http状态码
.validate(statusCode: 200..<300)
// 添加手工验证返回contentType类型
.validate(contentType: ["application/json"])
.responseJSON
{
response in
//调试打印返回信息
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
switch response.result
{
case.success(let json):
let dict = json as! Dictionary<String, AnyObject>
case.failure(let error):
print("\(error)")
}
}
调试打印信息如下:
{ URL: http://domain/contacts/postBatch/4 } { status code: 500, headers {
Connection = close;
"Content-Encoding" = gzip;
"Content-Length" = 1413;"Content-Type" = "text/html; charset=UTF-8";Date = "Mon, 18 Sep 2017 06:22:08 GMT";Server = Apache;Vary = "User-Agent,Accept-Encoding";
} }
3329 bytes
FAILURE
responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))
总结:此处是因为处理通讯录数据初始时没用清空数组数据 导致userId, phone数据重复,从而导致数据库
UNIQUE KEY `contactsPhone` (`userId`,`phone`) 唯一索引字段插入错误,产生500错误信息,而返回错误信息非json格式 导致解json序列化失败