Matteo Serpentoni
Matteo Serpentoni

Reputation: 573

Unable to convert data to string around character

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()
}

Upvotes: 1

Views: 3055

Answers (2)

Andrea Biagioni
Andrea Biagioni

Reputation: 174

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");

Upvotes: 4

T. Pasichnyk
T. Pasichnyk

Reputation: 732

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.

Upvotes: 1

Related Questions