问题
i'm trying to do an HTTP-Get request to get users on my database. After the data return from the request i use SwiftyJson library to get the users.
But, one of the users in his name contains the character "ò" and i received this error:
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 208."
UserInfo={NSDebugDescription=Unable to convert data to string around character 208.})
Here is my code:
func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let jsonData = data {
var parseError: NSError?
let json:JSON = JSON(data: jsonData, error: &parseError)
onCompletion(json, parseError)
} else {
onCompletion(nil, error)
}
})
task.resume()
}
回答1:
If i understood the problem, i think that you have to set the output of your servlet to UTF-8. An example, if you use a Java Servlet you can use
response.setCharacterEncoding("UTF-8");
回答2:
You have to ensure that that the character encoding being sent is identical to the one expected by the parser. I assume that your problem can be solved by encoding an output string with UTF-8.
You can validate your output using http://validator.w3.org, if you have a possibility to use a URL to your users DB.
来源:https://stackoverflow.com/questions/37658438/unable-to-convert-data-to-string-around-character