gpt4 book ai didi

json - IBM Watson Tone Analyzer API with Swift

转载 作者:行者123 更新时间:2023-12-01 04:41:29 26 4
gpt4 key购买 nike

我正在使用 Swift 处理 IBM Watson Tone Analyzer API。我尝试了以下代码:

override func viewDidLoad()
{
print("hello")
super.viewDidLoad()
let username = "USERNAME"
let password = "PASSWORD"
let versionDate = "2016-05-19" // use today's date for the most recent version
let service = ToneAnalyzer(username: username, password: password, version: versionDate)

let failure = { (error: NSError) in print(error) }
service.getTone("Text that you want to get the tone of", failure: failure) { responseTone in
print(responseTone.documentTone)
}

}

为此,我收到以下错误:Error Domain=com.alamofire.error Code=-6004 “无法序列化数据。无法解析 JSON 响应。序列化期间未提供错误信息。” UserInfo={NSLocalizedFailureReason=无法序列化数据。无法解析 JSON 响应。序列化期间未提供错误信息。

我阅读了文档,但对解决这个问题没有帮助。

最佳答案

您似乎在使用某种库?如果是这样,最可能的原因是版本号已更改,您需要对其进行设置。 More details about that here .

这是我编写的一些示例代码(请原谅我,我的 Swift 知识非常基础)。

//: Playground - noun: a place where people can play

import UIKit
var username = "<SERVICE USERNAME HERE>"
var password = "<SERVICE PASSWORD HERE>"
var endpoint = "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19&text="

var sampleText = "I am really excited to be working with Watson!"

// -------------------------------------------------------------------

var authString = username + ":" + password
var authData = authString.dataUsingEncoding(NSASCIIStringEncoding)
var authValue = "Basic " + authData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

var toneUrl = endpoint + sampleText.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
let url = NSURL(string: toneUrl)

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["Authorization" : authValue]
let session = NSURLSession(configuration: config)

var taskIsRunning = true;
let task = session.dataTaskWithURL(url!) {
(let data, let response, let error) in
if let httpResponse = response as? NSHTTPURLResponse {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

// Work with JSON object.
}
catch {
print("Problem serialising JSON object")
}
}
taskIsRunning = false
}

task.resume()
while (taskIsRunning) {
sleep(1)
}

关于json - IBM Watson Tone Analyzer API with Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37554664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com