在almofire swift ios中发布阿拉伯字符

时间:2018-04-09 09:39:32

标签: ios swift utf-8 alamofire

我一直试图使用almofire发布包含阿拉伯字符的参数。

当我发送拉丁字符时,一切都做得很好。

但是当我发送阿拉伯字符时,它看起来像这样? ???? ?? ?

我如何解决这个问题请帮助我。

我的代码:

var myUrls:String = "https//:wwww.mywebsite.com"
let parameters: Parameters = [
    "user_id": "\(is_loged)",
    "title": "\(title)",
    "msg": "\(msg)",
    "appKey": "\(staticsClass.AppKey)"

]


Alamofire.request(myUrls, method: .post, parameters: parameters ).responseJSON { response in
    print("Request: \(String(describing: response.request))")   // original url request
    print("Response: \(String(describing: response.response))") // http url response
    print("Result: \(response.result)")                         // response serialization result

    if let json = response.result.value {
        print("JSON: \(json)") // serialized json response
    }

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
        print("Data: \(utf8Text)") // original server data as UTF8 string
        let toast = Toast(text: NSLocalizedString("sent_successfully", comment: ""))
        toast.show()


    }
}

1 个答案:

答案 0 :(得分:0)

您需要将Content-Type标头设置为请求:

var myUrls = URLRequest(url: URL(string: "https//:wwww.mywebsite.com")!)
myUrls("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

Alamofire.request(myUrls, method: .post, parameters: parameters ).responseJSON { response in
    print("Request: \(String(describing: response.request))")   // original url request
    print("Response: \(String(describing: response.response))") // http url response
    print("Result: \(response.result)")                         // response serialization result

    if let json = response.result.value {
        print("JSON: \(json)") // serialized json response
    }

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
        print("Data: \(utf8Text)") // original server data as UTF8 string
        let toast = Toast(text: NSLocalizedString("sent_successfully", comment: ""))
        toast.show()


    }
}