私はAlamofireを初めて、Postmanから使用できるWebサービスを使用しようとしていますが、Alamofire経由では使用できません。郵便配達では、 "application/json"でHTTPヘッダキー "Accept"を設定することができ、Content-Type→application/jsonで応答が得られます。 charset = utf-8。 しかし、私がAlamofireを使って同じことをしようとすると、応答はxmlになるため、シリアル化は失敗します。 これには解決策がありますか?私はカスタムHTTPヘッダーを試しました& "Accept/Accept"キーが "application/json"に設定されているデフォルトセッションマネージャですが、意図した応答が得られません!ここにコードのサンプルがあります。Alamofire 4.4シリアライゼーションエラー、コンテンツタイプの問題
if let url = URL(string: "http://madinaty.demoday.us/api/1/lookup/stable/") {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = HTTPMethod.get.rawValue
urlRequest.addValue("application/json", forHTTPHeaderField: "content-type")
urlRequest.addValue("application/json", forHTTPHeaderField: "accept")
let request = Alamofire.request(urlRequest)
.responseJSON{ response in
print("Request: \(String(describing: response.request))") // original URL request
print("---------------------------")
print("HTTP URL response: \(String(describing: response.response))") // HTTP URL response
print("---------------------------")
print("Data: \(String(describing: response.data))") // server data
print("---------------------------")
print("Result of Reponse Serialization \(response.result)") // result of response serialization
print("---------------------------")
print("Error \(response.result.error)") // result of response serialization
print("---------------------------")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
これは私が取得error
です:
オプション(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(エラー文字 周り ドメイン= NSCocoaErrorDomainコード= 3840」無効な値を0 "。))))))))))
また、私はこれを試した:{NSDebugDescription =値が無効です値:
let header = ["content-type" : "application/json"]
Alamofire.request("http://madinaty.demoday.us/api/1/lookup/stable/",
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: header).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
}
どちらも機能しません。
この作品でしたどちら:
// get the default headers
var headers = Alamofire.SessionManager.defaultHTTPHeaders
// add your custom header
headers["Accept"] = "application/json"
// create a custom session configuration
let configuration = URLSessionConfiguration.default
// add the headers
configuration.httpAdditionalHeaders = headers
// create a session manager with the configuration
let sessionManager = Alamofire.SessionManager(configuration: configuration)
// make calls with the session manager
let request = sessionManager.request("http://madinaty.demoday.us/api/1/lookup/stable/")
.responseJSON { response in
debugPrint(response)
print("Result of Reponse Serialization \(response.result)")
}
debugPrint(request)
いくつかのコードを追加して、あなたが行ったことを見せてください。 – Simon
@ Simonちょうど – Taher
エラーオプション(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(エラードメイン= NSCocoaErrorDomainコード= 3840「文字0の周辺で値が無効です」)UserInfo = {NSDebugDescription =文字0の値が無効です} )))) – Taher