Unable to convert data to string around character

南楼画角 提交于 2019-12-11 06:09:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!